我遵循了http://www.xatik.com/2012/03/19/android-preloaded-database/上的代码,
但我不断得到NullPointerException
:
//my main activity class
public class PrepopSqliteDbActivity extends Activity {
ExternalDbOpenHelper bHelper;
private SimpleCursorAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Our key helper
bHelper = new ExternalDbOpenHelper(this);
bHelper.initializeDatabase("/data/data/com.catholic.arnold/databases/");
//all hymns are fetched
final Cursor cursor = bHelper.fetchAllHymns();
// The desired columns to be bound
String[] columns = new String[] {
ExternalDbOpenHelper.HYMN_ID,
ExternalDbOpenHelper.HYMN_NAME,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.tVid,
R.id.name,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.main,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.list);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
}
});
bHelper.close();
}
}
下面是我的日志:
08-17 11:54:59.360: E/AndroidRuntime(11549): FATAL EXCEPTION: main
08-17 11:54:59.360: E/AndroidRuntime(11549): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.catholic.arnold/com.catholic.arnold.PrepopSqliteDbActivity}: java.lang.NullPointerException
08-17 11:54:59.360: E/AndroidRuntime(11549): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-17 11:54:59.360: E/AndroidRuntime(11549): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-17 11:54:59.360: E/AndroidRuntime(11549): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-17 11:54:59.360: E/AndroidRuntime(11549): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-17 11:54:59.360: E/AndroidRuntime(11549): at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 11:54:59.360: E/AndroidRuntime(11549): at android.os.Looper.loop(Looper.java:137)
08-17 11:54:59.360: E/AndroidRuntime(11549): at android.app.ActivityThread.main(ActivityThread.java:5039)
08-17 11:54:59.360: E/AndroidRuntime(11549): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 11:54:59.360: E/AndroidRuntime(11549): at java.lang.reflect.Method.invoke(Method.java:511)
08-17 11:54:59.360: E/AndroidRuntime(11549): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-17 11:54:59.360: E/AndroidRuntime(11549): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-17 11:54:59.360: E/AndroidRuntime(11549): at dalvik.system.NativeStart.main(Native Method)
08-17 11:54:59.360: E/AndroidRuntime(11549): Caused by: java.lang.NullPointerException
08-17 11:54:59.360: E/AndroidRuntime(11549): at com.catholic.arnold.ExternalDbOpenHelper.fetchAllHymns(ExternalDbOpenHelper.java:110)
08-17 11:54:59.360: E/AndroidRuntime(11549): at com.catholic.arnold.PrepopSqliteDbActivity.onCreate(PrepopSqliteDbActivity.java:30)
08-17 11:54:59.360: E/AndroidRuntime(11549): at android.app.Activity.performCreate(Activity.java:5104)
08-17 11:54:59.360: E/AndroidRuntime(11549): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-17 11:54:59.360: E/AndroidRuntime(11549): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-17 11:54:59.360: E/AndroidRuntime(11549): ... 11 more