0

我正在尝试从数据库中获取值并使用支持者类中的函数将其显示在文本视图中。这两个类是 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),应用程序崩溃。我找不到任何理由。请帮忙。

堆栈跟踪:

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
4

0 回答 0