Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change the background of activity in fragment?
    primarykey
    data
    text
    <p>I want to change the background of activity in fragment. </p> <p>When I click the button in the <code>Fragment</code> , it choose the picture. And set the picture to the background of Activity.</p> <p>The following code is in Fragment:</p> <pre><code>public class MjpegPlayerFragment extends Fragment { private RelativeLayout relativeLayout; </code></pre> <p>public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {</p> <pre><code> 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); } } </code></pre> <p>But it crash , and the error log is like the following:</p> <pre><code>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} </code></pre> <p>And the <code>(MjpegPlayerFragment.java:1627)</code> is <code>relativeLayout.setBackground(background);</code></p> <p>Does someone can teach me how to solve this problem ? Thanks in abvance. </p> <p>----------------------------------------------EDIT------------------------------ Sorry , The above is not clear. </p> <p>I already add init the <code>relativeLayout</code> in <code>onCreatView</code> like the following:</p> <pre><code>relativeLayout = (RelativeLayout) getActivity().findViewById(R.id.splash); </code></pre> <p>And My scenario is:</p> <p>Activity1 --> Activity1 --> Fragment </p> <p>The splash is the RelativeLayout of Activity1.</p> <p>I want to change the background of Activity1 in Fragment.</p> <p>And the XML of Splash is like the following:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>I have two activity. The fragment is call by activity2 , and I want to change the background of activity1.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload