Android: Как программно изменить фон другого занятия?
Я пытаюсь изменить фон другого действия, передавая фотографию, которую они выбирают из фотогалереи своих телефонов, в макет. Однако я продолжаю получать ошибку:
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)
Я не понимаю, почему я получаю эту ошибку, потому что я делаю ту же самую вещь для текущей деятельности, в которой я нахожусь, и она прекрасно работает...
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
Вот мой фрагмент 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"/>
А вот и активность 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" />
1 ответ
Попробуй это
RelativeLayout home = (RelativeLayout) getFragmentManager().findFragmentById(R.id.navigation_drawer).getView().findViewById(R.id.home);