java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joericharduz.akita/com.joericharduz.akita.MainActivity}: java.lang.NullPointerException
コードは次のようになります。
Cursor cursor = null;
if (inputText == null || inputText.length() == 0) {
cursor = myDataBase.query(true, GL_TABLE, new String[] { GL_ID,
GL_FK, GL_LANG, GL_VALUE }, GL_FK + " like '%" + inputText
+ "%'", null, null, null, null, null);
} else {
cursor = myDataBase.query(true, GL_TABLE, new String[] { GL_ID,
GL_FK, GL_LANG, GL_VALUE }, GL_VALUE + " like '%"
+ inputText + "%'", null, null, null, null, null);
}
if (cursor != null) {
cursor.moveToFirst();
}
String[] columns = new String[] { GL_FK, GL_LANG, GL_VALUE };
int[] to = new int[] { R.id.tvWord, R.id.tvMeaning, R.id.tvKanji };
dataAdapter = new SimpleCursorAdapter(mContext, R.layout.listword,
cursor, columns, to, 0);
MainActivity mainActivity = new MainActivity();
ListView listView = mainActivity.wordlist;
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the
// result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String countryCode = cursor.getString(cursor
.getColumnIndexOrThrow("code"));
Toast.makeText(mContext, countryCode, Toast.LENGTH_SHORT)
.show();
}
});
私はこのチュートリアルに従いました。SQLite データベースからデータを取得し、リストビューに入れます。これをテストしようとすると、エラーが発生しました:java.lang.RuntimeException:アクティビティをインスタンス化できません。この問題の原因がわかりません。この問題を解決する方法をいくつか提案してください。
完全なスタック トレース:
08-05 21:36:01.375: W/dalvikvm(2367): threadid=1: thread exiting with uncaught exception (group=0x40018578)
08-05 21:36:01.382: E/AndroidRuntime(2367): FATAL EXCEPTION: main
08-05 21:36:01.382: E/AndroidRuntime(2367): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joericharduz.akita/com.joericharduz.akita.MainActivity}: java.lang.NullPointerException
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.os.Looper.loop(Looper.java:130)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-05 21:36:01.382: E/AndroidRuntime(2367): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 21:36:01.382: E/AndroidRuntime(2367): at java.lang.reflect.Method.invoke(Method.java:507)
08-05 21:36:01.382: E/AndroidRuntime(2367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-05 21:36:01.382: E/AndroidRuntime(2367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-05 21:36:01.382: E/AndroidRuntime(2367): at dalvik.system.NativeStart.main(Native Method)
08-05 21:36:01.382: E/AndroidRuntime(2367): Caused by: java.lang.NullPointerException
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.Activity.findViewById(Activity.java:1647)
08-05 21:36:01.382: E/AndroidRuntime(2367): at com.joericharduz.akita.MainActivity.<init>(MainActivity.java:20)
08-05 21:36:01.382: E/AndroidRuntime(2367): at java.lang.Class.newInstanceImpl(Native Method)
08-05 21:36:01.382: E/AndroidRuntime(2367): at java.lang.Class.newInstance(Class.java:1409)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
08-05 21:36:01.382: E/AndroidRuntime(2367): ... 11 more
08-05 21:37:13.851: W/dalvikvm(2579): threadid=1: thread exiting with uncaught exception (group=0x40018578)
08-05 21:37:13.937: E/AndroidRuntime(2579): FATAL EXCEPTION: main
08-05 21:37:13.937: E/AndroidRuntime(2579): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joericharduz.akita/com.joericharduz.akita.MainActivity}: java.lang.NullPointerException
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.os.Looper.loop(Looper.java:130)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-05 21:37:13.937: E/AndroidRuntime(2579): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 21:37:13.937: E/AndroidRuntime(2579): at java.lang.reflect.Method.invoke(Method.java:507)
08-05 21:37:13.937: E/AndroidRuntime(2579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-05 21:37:13.937: E/AndroidRuntime(2579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-05 21:37:13.937: E/AndroidRuntime(2579): at dalvik.system.NativeStart.main(Native Method)
08-05 21:37:13.937: E/AndroidRuntime(2579): Caused by: java.lang.NullPointerException
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.Activity.findViewById(Activity.java:1647)
08-05 21:37:13.937: E/AndroidRuntime(2579): at com.joericharduz.akita.MainActivity.<init>(MainActivity.java:20)
08-05 21:37:13.937: E/AndroidRuntime(2579): at java.lang.Class.newInstanceImpl(Native Method)
08-05 21:37:13.937: E/AndroidRuntime(2579): at java.lang.Class.newInstance(Class.java:1409)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
08-05 21:37:13.937: E/AndroidRuntime(2579): ... 11 more