在活动布局中,我使用的是 android.support.v4.widget.DrawerLayout。当用户从抽屉列表视图或菜单中选择选项时,我正在用片段替换主视图。
在某些时候,我需要更改现在布局中存在的片段中的数据,而不是替换片段。
ChannelFragment channelFragment = new ChannelFragment();
Fragment favsFragment = new FavorFragment();
Fragment homeFragment = new HomeFragment();`
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_base);
showFragment(channelFragment);
}
@Override
public void onSelectItem(int position) {
if(channels!=null){ //channels -- List<Channel>
showFragment(channelFragment);
//This will change the TextView text in channelFragment.
channelFragment.changeChannelTitle(channels.get(position).getName());
}
public void showFragment(Fragment fragment){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
}
ChannelFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_channel, container,
false);
programList = (ListView)rootView.findViewById(R.id.lvPrograms);
tvChannel = (TextView)rootView.findViewById(R.id.tvChannelName);
return rootView;
}
public void changeChannelTitle(final String title){
//i tried to set tvChannel..setText(title); it leads to the same exception
((TextView)getView().findViewById(R.id.tvChannelName)).setText(title);
}
片段通道.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvChannelName"
android:layout_width="wrap_content"``
android:layout_height="wrap_content"
android:text="Channels Page"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<ListView
android:id="@+id/lvPrograms"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
activity_base.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/listview_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
场景 1:我在 onCreate 中加载 channelFrgment 并尝试更改文本。改了,没问题。
场景 2:我在 onCreate 中加载主页片段。当用户从列表视图中选择一些选项时,我将主页片段替换为频道片段并尝试更改文本。在这种情况下会抛出空指针异常。我已经展示了已经初始化的片段并尝试了内容。但异常告诉 ChannelsFragment 中存在的 TextView 为空。
场景三:channelFragment(onCreate) -> Home Fragment -> ChannelFragment;现在我试图改变 textview 的文本,同样的异常正在发生。
我该如何处理这种情况?
日志输出
10-09 23:46:49.918: E/AndroidRuntime(21743): FATAL EXCEPTION: main
10-09 23:46:49.918: E/AndroidRuntime(21743): java.lang.NullPointerException
10-09 23:46:49.918: E/AndroidRuntime(21743): at com.ps.tvprograms.fragments.ChannelFragment.changeChannelTitle(ChannelFragment.java:53)
10-09 23:46:49.918: E/AndroidRuntime(21743): at com.ps.tvprograms.AppMainActivity.onSelectItem(AppMainActivity.java:72)
10-09 23:46:49.918: E/AndroidRuntime(21743): at com.ps.library.activities.menuimagesdynamic.DrawerActivity$DrawerItemClickListener.onItemClick(DrawerActivity.java:128)
10-09 23:46:49.918: E/AndroidRuntime(21743): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-09 23:46:49.918: E/AndroidRuntime(21743): at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
10-09 23:46:49.918: E/AndroidRuntime(21743): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)
10-09 23:46:49.918: E/AndroidRuntime(21743): at android.widget.AbsListView$1.run(AbsListView.java:3571)
10-09 23:46:49.918: E/AndroidRuntime(21743): at android.os.Handler.handleCallback(Handler.java:725)
10-09 23:46:49.918: E/AndroidRuntime(21743): at android.os.Handler.dispatchMessage(Handler.java:92)
10-09 23:46:49.918: E/AndroidRuntime(21743): at android.os.Looper.loop(Looper.java:153)
10-09 23:46:49.918: E/AndroidRuntime(21743): at android.app.ActivityThread.main(ActivityThread.java:5297)
10-09 23:46:49.918: E/AndroidRuntime(21743): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 23:46:49.918: E/AndroidRuntime(21743): at java.lang.reflect.Method.invoke(Method.java:511)
10-09 23:46:49.918: E/AndroidRuntime(21743): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-09 23:46:49.918: E/AndroidRuntime(21743): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-09 23:46:49.918: E/AndroidRuntime(21743): at dalvik.system.NativeStart.main(Native Method)