0
我知道這個問題已經被問了很多,我很抱歉問,但我完全失去了,並試圖學習這一點。NullPointerException Android的片段
所以我理解空指針的一般概念,但我在SImpleCursorAdpater上遇到了問題。我試圖用簡單的適配器來填充我的片段列表視圖,但我遇到了這個問題
在片段
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//opeing the DBApater
db = new DBAdapter(getActivity());
try {
db.open();
} catch (SQLException e) {
e.printStackTrace();
}
//opening the Recipes_Adapter
recipes = new Recipes_Adapter(getActivity());
try {
recipes.open();
} catch (SQLException e) {
e.printStackTrace();
}
Cursor cursor = recipes.getAllRecipes();
String[] columns = new String[] {Recipes_Adapter.NAME};
int[] to = new int[] {R.id.recipe_name};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getActivity(),R.layout.row,cursor, columns,to, 0);
ListView recipeList = (ListView)rootview.findViewById(R.id.recipe_list);
recipeList.setAdapter(myCursorAdapter); //error is on this line
}
R.layout.row
創建適配器<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/recipe_name"
android:paddingLeft="5dp"
android:textSize="17dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
配方Layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D1FFFF"
android:orientation="vertical">
<ListView
android:id="@+id/recipe_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="0.1dp"
android:divider="#0000CC"
>
</ListView>
</LinearLayout>
logcat的
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: FATAL EXCEPTION: main
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: Process: app.rory.menu, PID: 10625
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{app.rory.menu/app.rory.pocket_chef.MyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at app.rory.pocket_chef.Fragments.recipes_Fragment.onActivityCreated(recipes_Fragment.java:63)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.Fragment.performActivityCreated(Fragment.java:2061)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:912)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:834)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.Activity.performStart(Activity.java:6005)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-13 20:13:48.914 10625-10625/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
代碼看起來不錯,把你的logcat和刪除最後的關鍵字從ListView –
在哪裏設置Fragment的食譜佈局佈局? –
更新的問題,以顯示日誌 – Rory