简体   繁体   English

启动活动时出错

[英]Error when launching activity

I keep getting an error when I launch an activity, at first it was null pointer exception but now I have no idea what the error is and its just not working anymore 启动活动时,我总是收到错误消息,起初它是空指针异常,但现在我不知道错误是什么,并且它不再起作用

Logcat: Logcat:

02-18 13:15:28.885: D/dalvikvm(328): GC_EXTERNAL_ALLOC freed 50K, 53% free 2553K/5379K, external 2730K/3266K, paused 115ms
02-18 13:15:29.762: D/dalvikvm(328): GC_EXTERNAL_ALLOC freed 1K, 53% free 2552K/5379K, external 6764K/8447K, paused 51ms
02-18 13:15:45.312: D/dalvikvm(328): GC_EXTERNAL_ALLOC freed 11K, 52% free 2583K/5379K, external 8695K/10350K, paused 46ms
02-18 13:15:45.582: D/AndroidRuntime(328): Shutting down VM
02-18 13:15:45.582: W/dalvikvm(328): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-18 13:15:45.592: E/AndroidRuntime(328): FATAL EXCEPTION: main
02-18 13:15:45.592: E/AndroidRuntime(328): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.connectfour/com.example.connectfour.ScoreActivity}: java.lang.NullPointerException
02-18 13:15:45.592: E/AndroidRuntime(328):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-18 13:15:45.592: E/AndroidRuntime(328):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-18 13:15:45.592: E/AndroidRuntime(328):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-18 13:15:45.592: E/AndroidRuntime(328):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-18 13:15:45.592: E/AndroidRuntime(328):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 13:15:45.592: E/AndroidRuntime(328):  at android.os.Looper.loop(Looper.java:123)
02-18 13:15:45.592: E/AndroidRuntime(328):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 13:15:45.592: E/AndroidRuntime(328):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 13:15:45.592: E/AndroidRuntime(328):  at java.lang.reflect.Method.invoke(Method.java:507)
02-18 13:15:45.592: E/AndroidRuntime(328):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 13:15:45.592: E/AndroidRuntime(328):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 13:15:45.592: E/AndroidRuntime(328):  at dalvik.system.NativeStart.main(Native Method)
02-18 13:15:45.592: E/AndroidRuntime(328): Caused by: java.lang.NullPointerException
02-18 13:15:45.592: E/AndroidRuntime(328):  at com.example.connectfour.Database.getScores(Database.java:57)
02-18 13:15:45.592: E/AndroidRuntime(328):  at com.example.connectfour.ScoreActivity$MyCursorAdapter.<init>(ScoreActivity.java:21)
02-18 13:15:45.592: E/AndroidRuntime(328):  at com.example.connectfour.ScoreActivity.onCreate(ScoreActivity.java:49)
02-18 13:15:45.592: E/AndroidRuntime(328):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-18 13:15:45.592: E/AndroidRuntime(328):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-18 13:15:45.592: E/AndroidRuntime(328):  ... 11 more
02-18 13:15:54.303: I/Process(328): Sending signal. PID: 328 SIG: 9

Code

private static class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE" + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " INTEGER);"
                );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}
public Database(Context context) {
    ourContext = context;
}

public Database open() {
    ourHelper = new DBHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public Cursor getScores() {
    return ourDatabase.rawQuery("SELECT * FROM "+DATABASE_TABLE, null); //No Parameter
}
}

Code Score Activity: This is score activity line 21 is where the super thing is: 代码分数活动:这是分数活动第21行,超级内容是:

    @SuppressWarnings("deprecation")
    public MyCursorAdapter(Context context) {
        super(context, new Database(context).getScores());
        scoreIdx = getCursor().getColumnIndex(Database.KEY_NAME);
        // TODO Auto-generated constructor stub
    }

When you create your Database class you still have not instanciated the ourDatabase variable. 在创建数据库类时,您仍然没有实例化ourDatabase变量。

So when you're doing getScores you are going to have a NPE. 因此,当您执行getScores时,您将拥有一个NPE。

Just try to open() it before. 只需尝试将其打开()即可。

You have not initialized ourDatabase . 您尚未初始化ourDatabase first initialize it by calling open() ... 首先通过调用open()对其进行初始化。

public MyCursorAdapter(Context context) {
    super(context,  new Database(context).open().getScores(););
    scoreIdx = getCursor().getColumnIndex(Database.KEY_NAME);
    // TODO Auto-generated constructor stub
}

Try this. 尝试这个。

public Database open() {
  ourHelper = new DBHelper(ourContext);
   ourDatabase = ourHelper.getWritableDatabase();
  return this;
 }

public void close() {
    ourHelper.close();
}

public Cursor getScores() {
 ourDatabase = open();
 return ourDatabase.rawQuery("SELECT * FROM "+DATABASE_TABLE, null); //No Parameter
 close() ;
}

Here, when you call getScores, you db will be created and assigned to ourDatabase which then can be used easily. 在这里,当您调用getScores时,将创建您的数据库并将其分配给ourDatabase,然后可以轻松使用它。 Let me know if this works. 让我知道这个是否奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM