1

到目前为止,一切都照常工作:当我单击一个按钮时,我已经单击了一百次,突然我得到一个 NullPointerException。

这是相关代码:

View dbButton = findViewById(R.id.db_button);
dbButton.setOnClickListener(this);


public void onClick (View v) {
    switch (v.getId()) {
    case R.id.new_sms_button:
        Intent inte = new Intent(this, NewSMS.class);
        startActivity(inte);
        break;
    case R.id.about_button:
        Intent i = new Intent(this, About.class);
        startActivity(i);
        break;
    case R.id.db_button:
        Intent in = new Intent(this, Profile.class); duplicata
        startActivity(in);
        break;
    case R.id.prefs_button:
        Intent intent1 = new Intent(this, Settings.class);
        startActivity(intent1);
        break;
    }
}

这是 Profile 类:

public class Profile extends Activity {

private UsersData usersData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.userprofile);

    usersData = new UsersData(this);
    Cursor cursor = getUserData();
    showUserData(cursor);
}

private static String[] FROM = { PHONE_NUMBER, SMS_SENT, SMS_RECEIVED, };
private Cursor getUserData() {
    SQLiteDatabase db = usersData.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, null);
    startManagingCursor(cursor);
    return cursor;
}

private void showUserData(Cursor cursor) {
    StringBuilder builder = new StringBuilder("Data:\n");
    while (cursor.moveToNext()) {
        String num = cursor.getString(0);
        long smss = cursor.getLong(1);
        long smsr = cursor.getLong(2);
        builder.append("num: ").append(num).append("\n");
        builder.append("sent: ").append(smss).append("\n");
        builder.append("received: ").append(smsr).append("\n");
    }
    TextView text = (TextView) findViewById(R.id.userdata);
    text.setText(builder);
}
}

日志猫:

01-10 14:16:56.655: E/AndroidRuntime(2550): FATAL EXCEPTION: main
01-10 14:16:56.655: E/AndroidRuntime(2550): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.Profile}: java.lang.NullPointerException
01-10 14:16:56.655: E/AndroidRuntime(2550):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at android.os.Looper.loop(Looper.java:123)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at java.lang.reflect.Method.invoke(Method.java:507)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at dalvik.system.NativeStart.main(Native Method)
01-10 14:16:56.655: E/AndroidRuntime(2550): Caused by: java.lang.NullPointerException
01-10 14:16:56.655: E/AndroidRuntime(2550):     at com.rpgsms.Profile.showUserData(Profile.java:78)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at com.rpgsms.Profile.onCreate(Profile.java:50)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-10 14:16:56.655: E/AndroidRuntime(2550):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

UsersData 是应用程序的数据库类。知道为什么会这样吗?

4

2 回答 2

2

很难判断Profile.java:78是哪个语句,但它可能Cursornulldb.query(). 您需要添加更多错误检查并通过Log.e().

顺便说一句,您可以简化您的onClick()方法,而不是创建多个Intent具有不同名称的 s:

public void onClick (View v) {
    Intent intent = null;
    switch (v.getId()) {
    case R.id.new_sms_button:
        intent = new Intent(this, NewSMS.class);
        break;
    case R.id.about_button:
        intent = new Intent(this, About.class);
        break;
    case R.id.db_button:
        intent = new Intent(this, Profile.class);
        break;
    case R.id.prefs_button:
        intent = new Intent(this, Settings.class);
        break;
    default:
        break;
    }

    if (intent != null) {
        startActivity(intent);
    }
}
于 2013-01-10T13:24:17.000 回答
1

试着cursor.moveToFirst()在你的圈子之前打电话showUserData。如果这个方法返回false,那么你不会得到数据,而是试图处理它。

于 2013-01-10T13:34:47.277 回答