如何改变片段的活动背景? [英] How to change the background of activity in fragment?

查看:53
本文介绍了如何改变片段的活动背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改片段中活动的背景.

I want to change the background of activity in fragment.

当我单击Fragment中的按钮时,它将选择图片. 并将图片设置为活动的背景.

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

以下代码在Fragment中:

The following code is in Fragment:

public class MjpegPlayerFragment extends Fragment {

private RelativeLayout relativeLayout;

public View onCreateView(LayoutInflater充气机,ViewGroup容器,捆绑的saveInstanceState){

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}

(MjpegPlayerFragment.java:1627)relativeLayout.setBackground(background);

有人可以教我如何解决这个问题吗? 谢谢你.

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

----------------------------------------------编辑------------------------------ 抱歉,以上内容不清楚.

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

我已经在onCreatView中添加了relativeLayout的初始化,如下所示:

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

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

我的情况是:

Activity1-> Activity1->片段

Activity1 --> Activity1 --> Fragment

启动画面是Activity1的RelativeLayout.

The splash is the RelativeLayout of Activity1.

我想在Fragment中更改Activity1的背景.

I want to change the background of Activity1 in Fragment.

Splash的XML如下:

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>

我有两个活动.该片段由activity2调用,我想更改activity1的背景.

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

推荐答案

relativeLayout未初始化.是未初始化还是初始化失败.

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

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

在片段类的onActivityCreated中.

或在Activity类中初始化relativeLayout.使用界面作为活动的回调,并更改活动本身的背景

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

这篇关于如何改变片段的活动背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆