我认为这是一项容易的工作,但事实并非如此。我创建了一个子活动,其中有这些 ImageButtons 处理我在 MainActivity 中的背景图像的更改。
层次结构是:Main Activity > Settings Activity > ChangeStyle Activity
在我的 ChangeStyle 活动中,我尝试:
ImageButton leavesBtn = (ImageButton) findViewById(R.id.leavesBtn);
final FrameLayout mainFrameLyt = (FrameLayout) findViewById(R.id.mainFrameLayout);
leavesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mainFrameLyt.setBackgroundResource(R.drawable.ic_leaves_background);
Toast.makeText(getBaseContext(), "New style applied", Toast.LENGTH_SHORT).show();
}
});
日志猫:
05-26 02:22:43.308 1125-1125/com.myapplication2.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myapplication2.app, PID: 1125
java.lang.NullPointerException
at com.myapplication2.app.ChangeStyle$1.onClick(ChangeStyle.java:26)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
我应该改变什么?