0

I am trying to change the background of another activity by passing in the photo they select from their phones photo gallery to the layout. However I keep getting an error of :

Caused by: java.lang.NullPointerException
        at com.example.shawn.slidingexample.BackgroundOptionSlider.onActivityResult(BackgroundOptionSlider.java:105)
        at android.app.Activity.dispatchActivityResult(Activity.java:5778)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3710)

I dont understand why I am getting this error since I do he same exact thing for the current activity that I am in and it works fine...

InputStream inputStream = getContentResolver().openInputStream(selectedImage);
                Drawable uploadedPic = Drawable.createFromStream(inputStream, selectedImage.toString());
                RelativeLayout layout = (RelativeLayout) findViewById(R.id.updated_background);
                layout.setBackground(uploadedPic);//this works
                RelativeLayout home = (RelativeLayout) findViewById(R.id.home);
                home.setBackground(uploadedPic);//error here

Here is my fragment XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/home"
tools:context=".MainActivity$PlaceholderFragment">

<TextView android:id="@+id/menu_home" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="MENU HOME PAGE"/>

And here is the activity XML:

  <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
    android:layout_height="match_parent" />

    <fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.example.shawn.slidingexample.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

mr nooby noob
  • 1,860
  • 5
  • 33
  • 56
  • is `R.id.home` in the activity that gets the result photo? or its the other activity you are talking about? or its the fragment? in respect to the answer there, use `getSupportFragmentManager()` or why don't you just change it directly from the fragment is has activityonresult too – Elltz Apr 26 '15 at 23:30
  • @Elltz Yes it is. And I tried that, which is similar to what the other guy told me, but I still get a null pointer error for that line.. – mr nooby noob Apr 26 '15 at 23:47
  • is that the full error code ? – Elltz Apr 27 '15 at 00:19
  • @Elltz Here is the entire error: Caused by: java.lang.NullPointerException at com.example.shawn.slidingexample.BackgroundOptionSlider.onActivityResult(BackgroundOptionSlider.java:105) at android.app.Activity.dispatchActivityResult(Activity.java:5778) at android.app.ActivityThread.deliverResults(ActivityThread.java:3710)  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3757)             at android.app.ActivityThread.access$1400(ActivityThread.java:170)             at .... – mr nooby noob Apr 27 '15 at 00:48
  • well you could create a static Drawable variable in your fragment and set your drawable to that fragment and in your fragment onResume you could check if its not null then you set it as the background drawable then you set it to null, so you can preserve memory, idk if you like that – Elltz Apr 27 '15 at 02:04

1 Answers1

0

Try this

RelativeLayout home = (RelativeLayout) getFragmentManager().findFragmentById(R.id.navigation_drawer).getView().findViewById(R.id.home);
  • I tried that with home.setBackground(uploadedPic); following after it, but it still failed, getting a null pointer exception your code :/ – mr nooby noob Apr 26 '15 at 22:09