0

I thought that this was an easy job but it isn't. I've created a sub-activity in where there are these ImageButtons handling the change of my background image in my MainActivity.

The hierarchy is: Main Activity > Settings Activity > ChangeStyle Activity

In my ChangeStyle activity, I try with:

    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();
        }
    });

LOGCAT:

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)

What should I change?

Lampione
  • 1,622
  • 3
  • 21
  • 39

2 Answers2

0

You can't directly modify ones activity views from another one. You could solve this by use StartActivityForResult. An easier way would be to make a static method on MainActivity to change the background. This would store the new background in a static variable which is applied in the OnStart method.

wvdz
  • 16,251
  • 4
  • 53
  • 90
  • Thanks for your answer! :) So after making it I should call this static method from my style activity? What if I have more than 1 background image? – Lampione May 26 '14 at 07:04
  • The activity can only have 1 background at a time right? – wvdz May 26 '14 at 07:10
  • So you add a static variable to MainActivity `int backGroundResource`, with a static setter that takes a resourceId as parameter. – wvdz May 26 '14 at 07:49
0

That's because the frame layout you're looking for doesn't exist in the sub-activity's layout, so you get a NULL when you search for it. The right answer is to start the subactivity with startActivityForResult() and when the subactivity finishes you place the user's choices into the return intent. Then in onActivityResult read them in and set the backgrounds.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Wouldn't it gave me an error if I start my Style activity from my settings activity? Because I need to eventually put the result in the main activity – Lampione May 26 '14 at 07:22