[英]calling fragment from activity throws null pointer exception- Android
I am trying to call a List fragment from fragment activity, I m getting null pointer exception.Here is my code, 我试图从片段活动中调用列表片段,我得到了空指针异常。这是我的代码,
public class SampleEvents extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_events);
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_content);
if (fragment == null) {
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, new EventsList());
ft.commit();
}
}
Layout of fragment activity 片段活动的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Layout of the fragment event_list 片段event_list的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<TextView
android:id="@+id/empty_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#0c2c0c"
android:text="No Announcements Found"
android:textStyle="bold"
android:textSize="20sp" />
<ListView
android:id="@+id/events_list"
android:padding="10dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</RelativeLayout>
Fragment class 片段类
public class EventsList extends ListFragment implements
android.widget.AdapterView.OnItemClickListener{
View rootView;
static SharedPreferences sPrefs;
private NetWorkConnection nc;
private BaseAlerts alert;
private ListView events_listview;
private TextView emptyTV;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.events_list, container, false);
initUI();
uiListener();
return rootView;
}
Whats wrong with my code?Can someone help? 我的代码有什么问题?有人可以帮忙吗?
The log cat 日志猫
06-10 19:22:08.248: E/AndroidRuntime(1009): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ie.minoritybussiness/com.ie.minoritybussiness.dashboard.SampleEvents}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.os.Looper.loop(Looper.java:137)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-10 19:22:08.248: E/AndroidRuntime(1009): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 19:22:08.248: E/AndroidRuntime(1009): at java.lang.reflect.Method.invoke(Method.java:511)
06-10 19:22:08.248: E/AndroidRuntime(1009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-10 19:22:08.248: E/AndroidRuntime(1009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-10 19:22:08.248: E/AndroidRuntime(1009): at dalvik.system.NativeStart.main(Native Method)
06-10 19:22:08.248: E/AndroidRuntime(1009): Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ListFragment.ensureList(ListFragment.java:402)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ListFragment.onViewCreated(ListFragment.java:203)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:843)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
Content has view with id attribute 'android.R.id.list' that is not a ListView class
内容具有ID属性为“ android.R.id.list”的视图,该视图不是ListView类
That looks like your issue. 看起来像您的问题。 There are 2 reasons I can think of for this to be occuring.
我可以想到有两个原因。
You are giving the android id to a non-list view. 您正在将android id赋予非列表视图。 Check out your XML files for anywhere you are using
@android:id/list
as an id for a view. 在使用
@android:id/list
作为视图@android:id/list
任何地方检出XML文件。 Rename it to something else. 将其重命名为其他名称。
You are using a ListFragment
/ ListActivity
and are not defining a view with @android:id/list
as the id. 您正在使用
ListFragment
/ ListActivity
,并且未使用@android:id/list
作为ID定义视图。 These classes require that identifier. 这些类需要该标识符。 See this Stack Overflow post for some more details.
有关更多详细信息,请参见此Stack Overflow帖子 。
Put: 放:
FragmentManager fragmentManager = getSupportFragmentManager();
instead of: 代替:
FragmentManager fm = getFragmentManager();
Edit: If you are not using support library than this is probably not the reason for exception. 编辑:如果您不使用支持库,则这可能不是例外的原因。 I will give you example how do i do it:
我会给你一个例子,我该怎么做:
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = (ListFragment) fragmentManager
.findFragmentByTag("List");
if (fragment != null) {
fragmentManager.beginTransaction()
.add(R.id.fragment, fragment, "List").addToBackStack("List")
.commit();
}
So, i am not using findFragmentById. 因此,我没有使用findFragmentById。 I am using findFragmentTag and when i replace (or add) fragment, i put a tag ("List").
我正在使用findFragmentTag,当我替换(或添加)片段时,我放了一个标签(“列表”)。 ListFragment is custom fragment i created.
ListFragment是我创建的自定义片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.