-1

Good Day, I would like to allow user to change background in my app. Try to make it, but face with that problem:

12-09 06:22:15.874 20413-20413/com.vadimsleonovs.horoscope E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vadimsleonovs.horoscope, PID: 20413
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/20 flg=0x1 }} to activity {com.vadimsleonovs.horoscope/com.vadimsleonovs.horoscope.activities.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setBackground(android.graphics.drawable.Drawable)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3539)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
at android.app.ActivityThread.access$1300(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setBackground(android.graphics.drawable.Drawable)' on a null object reference
at com.vadimsleonovs.horoscope.activities.SettingsActivity.onActivityResult(SettingsActivity.java:76)
at android.app.Activity.dispatchActivityResult(Activity.java:6135)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) 
at android.app.ActivityThread.access$1300(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

The code is that:

public class SettingsActivity extends AppCompatActivity {

    Button mBcgBtn;
    private static int RESULT_LOAD_IMAGE = 1;
    String picturePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        mBcgBtn = (Button) findViewById(R.id.bcg_clr_btn);


        mBcgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
           try {
               Cursor cursor = getContentResolver().query(selectedImage,
                       filePathColumn, null, null, null);
               cursor.moveToFirst();
               int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
               picturePath  = cursor.getString(columnIndex);
               cursor.close();
           }catch(NullPointerException e){
               e.printStackTrace();
            }


            LinearLayout mHomeLayout = (LinearLayout) findViewById(R.id.activity_home);
            LinearLayout mDetailsPageLayout = (LinearLayout) findViewById(R.id.activity_details_page);


            Drawable d = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(picturePath));
            mHomeLayout.setBackground(d);
        }


    }

}

Also, I added write external storage permission, and this code works perfectly in "blank project", but in my project not working at all.

Vadim L.
  • 157
  • 2
  • 14

1 Answers1

1

It says mHomeLayout is null. ie, There is no LinearLayout with id activity_home in activity_settings layout.

So what you need to do is save the selected drawable path in storage preferences from Settings Activity. And load it when you open the other activity which has the Linearlayout with id activity_home. And then set it as it's background.

I hope that's your problem.

Midhun Vijayakumar
  • 2,927
  • 2
  • 19
  • 23