创建活动时崩溃

时间:2013-03-30 18:39:16

标签: android listview android-activity crash

我在更改为应用中的单独活动时遇到了一些问题。 被调用的活动被添加到清单中,所以这不是问题(本来就太容易了吧?)

这是我的主要活动,称之为麻烦的活动:

public class MainMenu extends Activity {

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

public void onBtnClick(final View view) {
    Intent intent = null;
    switch (view.getId()) {
    case R.id.btn_Start:
        ...//Working fine
        break;
    case R.id.btn_Options:
        ...//Also working
        break;
    case R.id.btn_Highscores:
        intent = new Intent(this, ScoresMenu.class);
        startActivity(intent);
        break;
    }
}
}

这就是所谓的活动:

public class ScoresMenu extends Activity {

String[] content;

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

    MyDB db = new MyDB(this);

    ListView lv = (ListView) findViewById(R.id.lv_Scores);

    content = db.getScores(); 
    //This returns a String[20], currently with one entry at [0]

    ArrayAdapter<String> scoreList = new ArrayAdapter<String>(this, 
            android.R.layout.simple_list_item_1, content);
    lv.setAdapter(scoreList);
    db.close();
}
}

这是请求的getScores():

public String[] getScores() { 

String[] scoreList = new String[20];

SQLiteDatabase db = this.getReadableDatabase(); 
Cursor cursor = db.query(
                TABLE_SCORES, 
                new String[] {KEY_ID, KEY_NAME, KEY_X, KEY_Y, KEY_A, KEY_TIME}, 
                null, null, null, null, 
                KEY_TIME + " ASC", 
                null);

if (cursor.moveToFirst()) { 
    int i = 1; 
    do { 
        String score; 
        String name = cursor.getString(1); 
        int x = Integer.valueOf(cursor.getString(2)); 
        int y = Integer.valueOf(cursor.getString(3)); 
        int a = Integer.valueOf(cursor.getString(4)); 
        String t = cursor.getString(5);

        if(i < 10) 
            score = "0" + i; 
        else 
            score = "" + i; score = score + ") " + name + " (" + x + "x" + y + " Felder mit " + a + " Minen in " + t + ")";

            scoreList[i] = score; i++;
    } while (cursor.moveToNext()); 
}
cursor.close(); 
return scoreList; 
} 
}

使用LogCat我到目前为止一直在追逐这个问题,所有事情一直持续到过去&#34; lv.setAdapter(scoreList);&#34;。所以我猜有一些错误,用scoreList填充ListView ......

LogCat本身给了我以下内容:

03-30 19:04:12.289: W/dalvikvm(9014): threadid=1: thread exiting with uncaught exception (group=0x40d7d8a8)
03-30 19:04:15.221: E/AndroidRuntime(9014): FATAL EXCEPTION: main
03-30 19:04:15.221: E/AndroidRuntime(9014): java.lang.NullPointerException
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.AbsListView.obtainView(AbsListView.java:2340)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.ListView.makeAndAddView(ListView.java:1793)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.ListView.fillDown(ListView.java:678)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.ListView.fillFromTop(ListView.java:739)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.ListView.layoutChildren(ListView.java:1638)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.AbsListView.onLayout(AbsListView.java:2170)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.View.layout(View.java:13846)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.ViewGroup.layout(ViewGroup.java:4466)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.View.layout(View.java:13846)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.ViewGroup.layout(ViewGroup.java:4466)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.View.layout(View.java:13846)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.ViewGroup.layout(ViewGroup.java:4466)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.View.layout(View.java:13846)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.ViewGroup.layout(ViewGroup.java:4466)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.View.layout(View.java:13846)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.ViewGroup.layout(ViewGroup.java:4466)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2156)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1914)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4616)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.Choreographer.doCallbacks(Choreographer.java:567)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.Choreographer.doFrame(Choreographer.java:536)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.os.Handler.handleCallback(Handler.java:615)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.os.Looper.loop(Looper.java:153)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at android.app.ActivityThread.main(ActivityThread.java:4987)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at java.lang.reflect.Method.invokeNative(Native Method)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at java.lang.reflect.Method.invoke(Method.java:511)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-30 19:04:15.221: E/AndroidRuntime(9014):     at dalvik.system.NativeStart.main(Native Method)
03-30 19:04:15.257: I/Process(9014): Sending signal. PID: 9014 SIG: 9

有人能在这里找出问题吗?

1 个答案:

答案 0 :(得分:0)

麻烦在于传递给ArrayAdapter的'content'。

  • 尝试记录内容变量并查看其是否具有预期值
  • 检查'db'的open()方法是否被调用