How to change the background of activity in a fragment?

I want to change the activity background in the fragment.

When I press the button in Fragment, she selects the image. And set the image to the Activity background.

The following code is in the snippet:

public class MjpegPlayerFragment extends Fragment {

private RelativeLayout relativeLayout;

public View onCreateView (inflatable LayoutInflater, ViewGroup container, Bundle savedInstanceState) {

    Log.d(TAG, "Fragment View Created") ;

    View view = inflater.inflate(R.layout.preview_player, container, false) ;
    relativeLayout = (RelativeLayout) getActivity().findViewById(R.id.splash);


ChangeBackground = (ImageButton) view.findViewById(R.id.ChangeBackground);

        ChangeBackground.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Activity activity = getActivity();
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, 1);
            }
        });
}
    @SuppressLint("NewApi")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if(resultCode == Activity.RESULT_OK) {

            Context context = getActivity();
            Uri uri = data.getData();
            ContentResolver cr = context.getContentResolver();
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
                BitmapDrawable background = new BitmapDrawable(bitmap);
                relativeLayout.setBackground(background);
            } catch (FileNotFoundException e) {
                // TODO: handle exception
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

But it fails, and the error log looks like this:

D/AndroidRuntime(29590): Shutting down VM
W/dalvikvm(29590): threadid=1: thread exiting with uncaught exception (group=0x416cc450)
--------- beginning of /dev/log/system
E/AndroidRuntime(29590): FATAL EXCEPTION: main
E/AndroidRuntime(29590): java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=1, result=-1, data=Intent { dat=content://media/external/images/media/23878 }} to activity {tw.com.a_i_t.IPCamViewer/tw.com.a_i_t.IPCamViewer.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(29590):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3161)
E/AndroidRuntime(29590):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3204)
E/AndroidRuntime(29590):    at android.app.ActivityThread.access$1100(ActivityThread.java:137)
E/AndroidRuntime(29590):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1254)
E/AndroidRuntime(29590):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(29590):    at android.os.Looper.loop(Looper.java:213)
E/AndroidRuntime(29590):    at android.app.ActivityThread.main(ActivityThread.java:4786)
E/AndroidRuntime(29590):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(29590):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(29590):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime(29590):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/AndroidRuntime(29590):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(29590): Caused by: java.lang.NullPointerException
E/AndroidRuntime(29590):    at tw.com.a_i_t.IPCamViewer.Viewer.MjpegPlayerFragment.onActivityResult(MjpegPlayerFragment.java:1627)
E/AndroidRuntime(29590):    at android.app.Activity.dispatchActivityResult(Activity.java:5196)
E/AndroidRuntime(29590):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3157)
E/AndroidRuntime(29590):    ... 11 more
W/ActivityManager(  568):   Force finishing activity tw.com.a_i_t.IPCamViewer/.MainActivity
W/ActivityManager(  568): Activity pause timeout for ActivityRecord{42260630 tw.com.a_i_t.IPCamViewer/.MainActivity}

And (MjpegPlayerFragment.java:1627)there isrelativeLayout.setBackground(background);

Can anyone teach me how to solve this problem? Thank you.

---------------------------------------------- EDIT --- --------------------------- Sorry, the above is not clear.

I already add init relativeLayoutin onCreatViewas follows:

relativeLayout = (RelativeLayout) getActivity().findViewById(R.id.splash);

And my scenario:

Activity1 → Activity1 → Fragment

Burst is RelativeLayout Activity1.

Activity1 .

XML Splash :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/splash"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/wifi_dvr" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/title"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="146dp"
        android:src="@drawable/banner" />

</RelativeLayout>

. activity2, activity1.

0
2

relativeLayout . , .

relativeLayout = (RelativeLayout)getActivity().findViewById(R.id.relativelayout);

onActivityCreated .

relativeLayout Activity.

+1

-

RelativeLayout relativeLayout;

onCreate() Activity

relativeLayout=(RelativeLayout)findViewById(R.id.relativelayout);

getActiviy().relativeLayout.setBackground(background);
0

All Articles