单击按钮后出现NullPointerException

时间:2013-01-10 13:15:16

标签: android exception

到目前为止,一切都像往常一样工作:当我点击一个按钮时,我点击了一百次,突然间我得到一个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);
}
}

logcat的:

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是应用程序的数据库类。知道为什么会这样吗?

2 个答案:

答案 0 :(得分:2)

很难判断哪个语句Profile.java:78,但Cursor参数null可能是由db.query()中的失败引起的Log.e()参数。您需要添加更多错误检查并通过onClick()报告。

顺便说一下,您可以简化Intent方法,而不是创建具有不同名称的多个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); } }

{{1}}

答案 1 :(得分:1)

尝试在cursor.moveToFirst()的圈子之前致电showUserData。如果此方法返回false,那么您不会获取数据,而是尝试处理它。