我正在尝试从数据库中获取值,并使用支持者类中的函数将其显示在textview中。这两个类是MainActivity.java& DbHelper.java。
MainActivity中的Onclick功能是:
public void see(View v){
dbhelper.seeRecord(s, ss);
//tv.append(s,ss);
}
DbHelper中用于检索数据的函数是:
private String []cols = new String[]{"name","id"};
public void seeRecord(String a, int b){
db= this.getReadableDatabase();
Cursor c = db.rawQuery("select * from vijay1", null);
if(c.moveToFirst()){
while(c.moveToNext()){
String nameintable = c.getString(c.getColumnIndex(cols[0]));
int idintable = c.getInt(c.getColumnIndex(cols[1]));
a=nameintable;
b=idintable;
**main.tv.append(b+". "+a+"\n");** //MAIN IS OBJECT OF ACTIVITY CLASS
}
c.close();
db.close();
}
}
现在,如果我将值附加到onclick函数中的textview,则会显示最后输入的db值,这很明显。
但是如果我将它附加到DbHelper类的while循环中(这样每行都会将值附加到textview),App崩溃了。我找不到任何理由。请帮助。
堆栈跟踪:
07-31 12:50:01.537: E/AndroidRuntime(486): FATAL EXCEPTION: main
07-31 12:50:01.537: E/AndroidRuntime(486): java.lang.IllegalStateException: Could not execute method of the activity
07-31 12:50:01.537: E/AndroidRuntime(486): at android.view.View$1.onClick(View.java:2072)
07-31 12:50:01.537: E/AndroidRuntime(486): at android.view.View.performClick(View.java:2408)
07-31 12:50:01.537: E/AndroidRuntime(486): at android.view.View$PerformClick.run(View.java:8816)
07-31 12:50:01.537: E/AndroidRuntime(486): at android.os.Handler.handleCallback(Handler.java:587)
07-31 12:50:01.537: E/AndroidRuntime(486): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 12:50:01.537: E/AndroidRuntime(486): at android.os.Looper.loop(Looper.java:123)
07-31 12:50:01.537: E/AndroidRuntime(486): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-31 12:50:01.537: E/AndroidRuntime(486): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 12:50:01.537: E/AndroidRuntime(486): at java.lang.reflect.Method.invoke(Method.java:521)
07-31 12:50:01.537: E/AndroidRuntime(486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-31 12:50:01.537: E/AndroidRuntime(486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-31 12:50:01.537: E/AndroidRuntime(486): at dalvik.system.NativeStart.main(Native Method)
07-31 12:50:01.537: E/AndroidRuntime(486): Caused by: java.lang.reflect.InvocationTargetException
07-31 12:50:01.537: E/AndroidRuntime(486): at com.example.databaseaccess.MainActivity.see(MainActivity.java:44)
07-31 12:50:01.537: E/AndroidRuntime(486): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 12:50:01.537: E/AndroidRuntime(486): at java.lang.reflect.Method.invoke(Method.java:521)
07-31 12:50:01.537: E/AndroidRuntime(486): at android.view.View$1.onClick(View.java:2067)
07-31 12:50:01.537: E/AndroidRuntime(486): ... 11 more
07-31 12:50:01.537: E/AndroidRuntime(486): Caused by: java.lang.NullPointerException
07-31 12:50:01.537: E/AndroidRuntime(486): at com.example.databaseaccess.DbHelper.seeRecord(DbHelper.java:78)
07-31 12:50:01.537: E/AndroidRuntime(486): ... 15 more