Android Fragment,ListView NullPointer异常

时间:2014-07-12 12:59:46

标签: android nullpointerexception cursor fragment simplecursoradapter

我在Fragment Class中创建一个ListView,它可以简单地列出来自app数据库的联系人。因为我刚刚创造它,所以我没有任何想象力。目前我收到一个NullPointer错误,看起来它指向onCreate,但我看不到错误。也许你们会的。

public class ContactsFragment extends ListFragment {

    Context fContext;
    private DatabaseHandler db;
    private SimpleCursorAdapter contactCursorAdapter;

    public ContactsFragment(){}
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        fContext = getActivity();
        View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);

        //Generate ListView from SQLite Database
        contactListView();
        return rootView;
    }

    private void contactListView() {

        DatabaseHandler db = new DatabaseHandler(fContext);
        Cursor contactCursor = db.fetchContacts();

        // Allow activity to manage the lifetime of the cursor
        //startManagingCursor(contactCursor);

        //Setup mapping of cursor
        String[] fromFields = new String[]
                {db.KEY_NAMECONCAT,
                db.KEY_COMPANY};
        int[] toViewIds = new int[]
                {R.id.tvFullName,
                R.id.tvContactCompany};

        //Create Adapter
        @SuppressWarnings("deprecation")
        SimpleCursorAdapter contactCursorAdapter = new SimpleCursorAdapter(
                fContext,
                R.layout.contact_list_item,
                contactCursor,
                fromFields,
                toViewIds
                );

        //Set the adapter for the List View
        ListView contactList = (ListView) rootView.findViewById(R.id.listContacts);
        contactList.setAdapter(contactCursorAdapter);

    }

    @Override
    public void onPause() {
        super.onPause();
        db.close();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        db.close();
    }

}

fragment_contacts.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listContacts"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ListView>

contact_list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <TextView
        android:id="@+id/tvFullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:text="FullName"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/tvContactCompany"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvFullName"
        android:layout_below="@+id/tvFullName"
        android:paddingLeft="10dp"
        android:text="Company" />

</RelativeLayout> 

logcat的:

 FATAL EXCEPTION: main
  Process: com.garudaapps.myapp, PID: 30894
  java.lang.NullPointerException
    at com.garudaapps.myapp.ContactsFragment.contactListView(ContactsFragment.java:89)
    at com.garudaapps.myapp.ContactsFragment.onCreateView(ContactsFragment.java:35)
    at android.app.Fragment.performCreateView(Fragment.java:1700)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
    at android.app.BackStackRecord.run(BackStackRecord.java:684)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5356)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

您已在错误的位置初始化列表引用。

public class ContactsFragment extends ListFragment {

    Context fContext;
    private DatabaseHandler db;
    private SimpleCursorAdapter dataAdapter;

    public ContactsFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        fContext = getActivity();
        View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);

        //Generate ListView from SQLite Database
        ListView contactList = (ListView) rootView.findViewById(R.id.listContacts);
        contactListView();
        contactList.setAdapter(contactCursorAdapter);
        return rootView;
    }

    private void contactListView() {

        DatabaseHandler db = new DatabaseHandler(fContext);
        Cursor contactCursor = db.fetchContacts();

        // Allow activity to manage the lifetime of the cursor
        //startManagingCursor(contactCursor);

        //Setup mapping of cursor
        String[] fromFields = new String[]
                {db.KEY_NAMECONCAT,
                        db.KEY_COMPANY};
        int[] toViewIds = new int[]
                {R.id.tvFullName,
                        R.id.tvContactCompany};

        //Create Adapter
        @SuppressWarnings("deprecation")
        SimpleCursorAdapter contactCursorAdapter = new SimpleCursorAdapter(
                fContext,
                R.layout.contact_list_item,
                contactCursor,
                fromFields,
                toViewIds
        );

    }

    @Override
    public void onPause() {
        super.onPause();
        db.close();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        db.close();
    }

}