从活动调用片段会抛出空指针异常 - Android

时间:2014-06-10 13:01:57

标签: java android android-fragments

我试图从片段活动中调用List片段,我得到空指针异常。这是我的代码,

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(); 
        }


    }

片段活动的布局

<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>

片段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>

片段类

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;
}

我的代码出了什么问题?有人可以帮忙吗?

日志猫

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)

2 个答案:

答案 0 :(得分:3)

  

内容具有id属性'android.R.id.list'的视图,该视图不是ListView类

这看起来像是你的问题。我可以想到有两个原因可以解决这个问题。

  1. 您将android id提供给非列表视图。查看您使用@android:id/list作为视图ID的任何地方的XML文件。将其重命名为其他内容。

  2. 您使用ListFragment / ListActivity定义@android:id/list作为ID的视图。这些类需要该标识符。有关更多详细信息,请参阅this Stack Overflow post

答案 1 :(得分:0)

把:

FragmentManager fragmentManager = getSupportFragmentManager();

而不是:

FragmentManager fm = getFragmentManager();

修改 如果您没有使用支持库,则可能不是异常的原因。我会举例说明如何做到这一点:

FragmentManager fragmentManager = getSupportFragmentManager();

Fragment fragment = (ListFragment) fragmentManager
            .findFragmentByTag("List");

if (fragment != null) {
    fragmentManager.beginTransaction()
        .add(R.id.fragment, fragment, "List").addToBackStack("List")
        .commit();
}

所以,我没有使用findFragmentById。我正在使用findFragmentTag,当我替换(或添加)片段时,我放了一个标签(“List”)。 ListFragment是我创建的自定义片段。