更改活动prevents的ImageButton setImageResource从工作工作、prevents、ImageButton、setImageResource

2023-09-12 23:44:52 作者:{丆尛吢ノ

我有改变对的ImageButton 在图像上每一个点击一个主要活动。

I have a main activity that changes the image on the ImageButton on every click.

public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        static ImageButton mGetClickTime;
            mGetClickTime.setOnClickListener(new View.OnClickListener() {
            mUpdateBackground();
            }
        }
    }

public static void mUpdateBackground() {
        int[] imageIds = { 
                R.drawable.bg1,
                R.drawable.bg2,
                R.drawable.bg3,
                R.drawable.bg4,
                };
        Random generator = new Random();
        randomImageId = imageIds[generator.nextInt(imageIds.length)];
                mGetClickTime.setImageResource(randomImageId);
        }

这工作得很好,直到点击菜单按钮和其他活动访问

This works fine, until the menu button is clicked and another activity is accessed

public class settings extends MainActivity{


@Override
   public void onCreate(Bundle savedInstanceState) {                
   super.onCreate(savedInstanceState);    
   setContentView(R.layout.settings);
   }
}

在菜单或后退按钮是pressed,我们回到主要活动。但是,一旦发生这种情况,将的ImageButton 不再更新,每次点击的形象。其他一切仍然有效,包括文本框更新上每一次点击。

When the menu or back button is pressed, we return to the main activity. But once this has happened, the ImageButton no longer has the image updated with every click. Everything else still works, including text boxes updating on each click.

我是什么做错了吗?

修改的

感谢您的帮助。目前,它似乎继续工作后,我从子活动返回。 (万岁!)

Thanks for all your help. At the moment, it seems to continue working after I return from the sub-activity. (Hooray!)

但现在我无法得到 mUpdateBackground onResume时调用的方法来工作(),或的其它活动。下面是我在哪里:

But now I can't get the mUpdateBackground method to work when called from onResume(), or from the other activity. Here's where I'm at:

1 public class MainActivity extends Activity {
2   public static ImageButton mGetClickTime;
3 @Override
4   protected void onResume() {
5        super.onResume();
6       //Get shared preferences
7               mSharedPreferences = getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE);
8           dp = mSharedPreferences.getInt("DecimalPlaces", 0);
9               length_setting = mSharedPreferences.getInt("MSSelector", 1);
10              backgroundPic = mSharedPreferences.getBoolean("BackgroundPic", true);
11              //mUpdateBackground();  
12          }
12  @Override
13  protected void onCreate(Bundle savedInstanceState) {
14      super.onCreate(savedInstanceState);
15      setContentView(R.layout.activity_main);
16
17      //Get shared preferences
18      mSharedPreferences = getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE);
19      dp = mSharedPreferences.getInt("DecimalPlaces", 0);
20      length_setting = mSharedPreferences.getInt("MSSelector", 5);
21      mUpdateBackground();
22          mGetClickTime.setOnClickListener(new View.OnClickListener() {
23      mUpdateBackground();
24          }
25     }
26 }
27  public void mUpdateBackground() {
28      if (backgroundPic) {
29          int[] imageIds = { 
30                  R.drawable.bg1,
31                  R.drawable.bg2,
32                  R.drawable.bg3,
33                  R.drawable.bg4,
34
35          };
36          Random generator = new Random();
37          randomImageId = imageIds[generator.nextInt(imageIds.length)];
38          Log.d("1", "backgroundPic: "+randomImageId);
39      }
40      else {
41          randomImageId = R.drawable.bg0;
42          Log.d("1", "backgroundPicCALLED: "+randomImageId);
43      }
44      mGetClickTime = (ImageButton) findViewById(R.id.clicker);
45      mGetClickTime.setImageResource(randomImageId);
46  }

我的问题与此是,如果我去掉11行,我收到了 NullPointerException异常。莫非是因为 mUpdateBackground()法在第27行是不是静态?如果我让静态然后在第44行,我得到的 findViewById 的错误,不能让一个静态引用非静态方法 findViewById(INT)从类型活动。我真的难住了。我显然不是按照逻辑正确,但我已经走遍了几次,我看不到有什么它可能是。

The problem I have with this is that if I uncomment line 11 I get a NullPointerException. Could that be because the mUpdateBackground() method at line 27 isn't static? If I make it static then at line 44 I get an error on findViewById, Cannot make a static reference to the non-static method findViewById(int) from the type Activity. I'm really stumped. I'm obviously not following the logic correctly but I've gone over it a few times and I can't see what it could be.

推荐答案

哈,现在我明白是什么在困扰你。你的问题是,无论是onResume或发生的onCreate第二次,这将重新设置图片为您在XML设置默认值。你可以简单地登录并查看哪些这些发生。当你看到你的形象返回到默认值,只需在您的活动类,它会记住您当前的图片ID或你想要的创建领域。事情是这样的。

Ha, now I see what is bothering you. Your problem is either onResume or onCreate happens second time and this resets your image to your default value you set in your XML. You can simply log and see which of these occurs. Once you see where your image is returned to default, simply create field in your activity class which will remember your current image id or what you want. Something like this

private int currentImage = R.drawable.firstImage;

public class MainActivity extends Activity{

   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

    static ImageButton mGetClickTime;
    mGetClickTime.setImageResource(currentImage);
        mGetClickTime.setOnClickListener(new View.OnClickListener() {
        mUpdateBackground();
        }
    }
   }

public static void mUpdateBackground() {
    int[] imageIds = { 
            R.drawable.bg1,
            R.drawable.bg2,
            R.drawable.bg3,
            R.drawable.bg4,
            };
    Random generator = new Random();
    randomImageId = imageIds[generator.nextInt(imageIds.length)];
            mGetClickTime.setImageResource(randomImageId);
            currentImage = randomImageId;
    }

希望这有助于和享受你的工作。

Hope this helps and enjoy your work.