我有一项主要活动,可以在每次点击时更改ImageButton
上的图片。
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);
}
这很好用,直到单击菜单按钮并访问另一个活动
public class settings extends MainActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
}
}
按下菜单或后退按钮后,我们将返回主要活动。但是一旦发生这种情况,ImageButton
不再每次点击都会更新图像。其他一切仍然有效,包括每次点击更新的文本框。
我做错了什么?
修改的
感谢您的帮助。目前,我从子活动返回后似乎继续工作。 (万岁!)
但是现在我从mUpdateBackground
或其他活动调用时无法使onResume()
方法正常工作。这就是我所在的地方:
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
。可能是因为第27行的mUpdateBackground()
方法不是static
?如果我将其设为static
,那么在第44行我会在findViewById
上收到错误,无法对类型static
方法non-static
进行findViewById(int)
引用{1}}。我真的很难过。我显然没有正确地遵循逻辑,但我已经过了几次而且我看不出它可能是什么。
答案 0 :(得分:1)
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;
}
希望这有助于并享受您的工作。
答案 1 :(得分:0)
我从这个帖子得到了答案:Android: What's the best hierarchy for this app?
关键是学习在调用之前检查对象是否为null的概念。
我使用的代码是:
if (mGetClickTime != null) {
Log.d("mGetClickTime: ", "is not null");
mGetClickTime.setImageResource(randomImageId);
}
else {
mGetClickTime = (ImageButton) findViewById(R.id.clicker);
mGetClickTime.setImageResource(randomImageId);
Log.d("mUpdateBackground", " was null");
}