我有一个应用程序,我尝试从数据库表中选择行(如果存在)。但我在应用程序中有错误。该表不为空,我在表中插入了一行。
代码:
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.ll);
TextView tv1 = (TextView)findViewById(R.id.textView1);
TextView tv2 = (TextView)findViewById(R.id.textView2);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(this);
Cursor cursor = sqdb.rawQuery("SELECT _id, One, Two FROM TextView", null);
if(cursor!= null && cursor.getCount() > 0)
{
cursor.moveToFirst();
while(!cursor.isAfterLast())
{
LayoutInflater inf = getLayoutInflater();
View v = inf.inflate(R.layout.test, null, false);
tv1.setId(cursor.getInt(0));
tv2.setId(1000 - cursor.getInt(0));
tv1.setText(cursor.getString(1));
tv2.setText(cursor.getString(2));
registerForContextMenu(tv1);
registerForContextMenu(tv2);
//((TextView)v.findViewById(R.id.textView1)).setText(String.valueOf(i));
//((TextView)v.findViewById(R.id.textView2)).setText(String.valueOf(1000-i));
ll.addView(v);
}
}
Logcat日志:
- 08-29 12:25:22.155: E/Your TAG(6970): Your Message
- 08-29 12:25:22.155: E/Your TAG(6970): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nightingale8/com.example.nightingale8.MainActivity}: java.lang.NullPointerException
- 08-29 12:25:22.155: E/Your TAG(6970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
- 08-29 12:25:22.155: E/Your TAG(6970): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
- 08-29 12:25:22.155: E/Your TAG(6970): at android.app.ActivityThread.access$600(ActivityThread.java:140)
- 08-29 12:25:22.155: E/Your TAG(6970): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
- 08-29 12:25:22.155: E/Your TAG(6970): at android.os.Handler.dispatchMessage(Handler.java:99)
- 08-29 12:25:22.155: E/Your TAG(6970): at android.os.Looper.loop(Looper.java:137)
- 08-29 12:25:22.155: E/Your TAG(6970): at android.app.ActivityThread.main(ActivityThread.java:4898)
- 08-29 12:25:22.155: E/Your TAG(6970): at java.lang.reflect.Method.invokeNative(Native Method)
- 08-29 12:25:22.155: E/Your TAG(6970): at java.lang.reflect.Method.invoke(Method.java:511)
- 08-29 12:25:22.155: E/Your TAG(6970): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
- 08-29 12:25:22.155: E/Your TAG(6970): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
- 08-29 12:25:22.155: E/Your TAG(6970): at dalvik.system.NativeStart.main(Native Method)
- 08-29 12:25:22.155: E/Your TAG(6970): Caused by: java.lang.NullPointerException
- 08-29 12:25:22.155: E/Your TAG(6970): at com.example.nightingale8.MainActivity.startNormalActivity(MainActivity.java:79)
- 08-29 12:25:22.155: E/Your TAG(6970): at com.example.nightingale8.MainActivity.onCreate(MainActivity.java:55)
- 08-29 12:25:22.155: E/Your TAG(6970): at android.app.Activity.performCreate(Activity.java:5206)
- 08-29 12:25:22.155: E/Your TAG(6970): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
- 08-29 12:25:22.155: E/Your TAG(6970): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
- 08-29 12:25:22.155: E/Your TAG(6970): ... 11 more