Je crée un ListView dans ma classe de fragmentation qui liste simplement les contacts de la base de données de l'application. Je n'ai rien d'extraordinaire depuis que je suis en train de le créer. Pour l'instant, j'obtiens une erreur NullPointer qui ressemble à l'onCreate, mais je ne vois pas l'erreur. peut-être que vous le ferez.Fragment Android, Exception ListView NullPointer
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)
Il se pourrait que le contexte est erroné ou il pourrait être ce qu'il est normalement et il y a un élément dans le code qui n'existe pas vraiment. De toute façon, je ne vois tout simplement pas –