gpt4 book ai didi

android - 创建 Activity 时崩溃

转载 作者:行者123 更新时间:2023-12-02 06:30:57 26 4
gpt4 key购买 nike

我在更改为应用程序中的单独 Activity 时遇到一些问题。被调用的 Activity 已添加到 list 中,因此这不是问题(太简单了,对吧?)

这是我的主要 Activity ,它调用了麻烦的 Activity :

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;
}
}
}

这就是所谓的 Activity :

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,我到目前为止一直在追寻问题,一切都运行到过去的“lv.setAdapter(scoreList);”。所以我猜想有一些错误,用 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

有人能解决这里的问题吗?

最佳答案

问题在于传递给 ArrayAdapter 的“内容”。

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

关于android - 创建 Activity 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15722065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com