How to change the background of activity in fragment?

若如初见. 提交于 2019-12-13 04:13:10

问题


I want to change the background of activity in fragment.

When I click the button in the Fragment , it choose the picture. And set the picture to the background of Activity.

The following code is in Fragment:

public class MjpegPlayerFragment extends Fragment {

private RelativeLayout relativeLayout;

public View onCreateView(LayoutInflater inflater, 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 crash , and the error log is like the following:

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 the (MjpegPlayerFragment.java:1627) is relativeLayout.setBackground(background);

Does someone can teach me how to solve this problem ? Thanks in abvance.

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

I already add init the relativeLayout in onCreatView like the following:

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

And My scenario is:

Activity1 --> Activity1 --> Fragment

The splash is the RelativeLayout of Activity1.

I want to change the background of Activity1 in Fragment.

And the XML of Splash is like the following:

<?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>

I have two activity. The fragment is call by activity2 , and I want to change the background of activity1.


回答1:


relativeLayout is not initialized. Either is it not initialized or the initialization fails.

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

in onActivityCreated of fragment class.

Or Initialize relativeLayout in Activity class. Use a interface as a callback to the activity and change the background in activity itself




回答2:


try something like this

declare RelativeLayout relativeLayout; in Activity

And in onCreate() of Activity

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

then in Fragment

getActiviy().relativeLayout.setBackground(background);


来源:https://stackoverflow.com/questions/20857105/how-to-change-the-background-of-activity-in-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!