Calling snippet from activity throws null-pointer-android-exception

I'm trying to call a list fragment from a fragment activity, I get a 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(); 
        }


    }

      

Fragment action layout

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

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

      

What's wrong with my code? Can anyone please help?

Cat magazine

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)

      

0


source to share


2 answers


Content has a view with id attribute 'android.R.id.list' which is not a ListView class

This looks like your problem. There are two reasons why I might think this is happening.



  • You are specifying android id for a non-list view. Check your XML files anywhere you use @android:id/list

    as an identifier for a view. Rename it to something else.

  • You use ListFragment

    / ListActivity

    and not by defining a view with @android:id/list

    as an identifier. These classes require this ID. See this post for details .

+3


source


Placed:

FragmentManager fragmentManager = getSupportFragmentManager();

      

instead:

FragmentManager fm = getFragmentManager();

      



Edit: If you are not using the support library, this is probably not the reason for the exception. I will give you an example how to 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. I am using findFragmentTag and when I replace (or add) a fragment, I put a tag ("List"). ListFragment is the custom fragment you created.

0


source







All Articles