gpt4 book ai didi

java - Android 代码错误导致应用程序意外停止

转载 作者:行者123 更新时间:2023-12-01 11:28:40 24 4
gpt4 key购买 nike

以下是我从开源修改的代码,下面是显示错误的logcat,我检查了代码很多次,但不知道问题出在哪里。请帮忙解决。

/**
* MainActivity
*/
public class MainActivity extends AbstractMainActivity implements
Memory.OnMemoryListener {

private static final int[] tiles_fruits = { R.drawable.a1, R.drawable.a2,
R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6,
R.drawable.a7, R.drawable.a8, R.drawable.a9, R.drawable.a10,
R.drawable.a11, R.drawable.a12, R.drawable.a13, R.drawable.a14,
R.drawable.a15, R.drawable.a16, R.drawable.a17, R.drawable.a18,
R.drawable.a19, R.drawable.a20, R.drawable.a21, R.drawable.a22 };

private static final int[] tiles_foods = { R.drawable.d1, R.drawable.d2,
R.drawable.d3, R.drawable.d4, R.drawable.d5, R.drawable.d6,
R.drawable.d7, R.drawable.d8, R.drawable.d9, R.drawable.d10,
R.drawable.d11, R.drawable.d12 };

private static final int[][] icons_set = { tiles_fruits, tiles_foods };

private static final int[] sounds = { R.raw.gupp, R.raw.winch,
R.raw.chtoing, R.raw.kito, R.raw.kato, R.raw.ding, R.raw.ding2,
R.raw.ding3, R.raw.ding4, R.raw.ding5, R.raw.ding6, R.raw.dong,
R.raw.swirlup, R.raw.swipp };

private static final int[] not_found_tile_set = { R.drawable.not_found_fruits, R.drawable.not_found_foods };
private Memory mMemory;
// private int mNotFoundResId;
private MemoryGridView mGridView;

/**
* {@inheritDoc }
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

PreferencesService.init(this);
newGame();
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}

/**
* {@inheritDoc }
*/
@Override
protected View getGameView() {
return mGridView;
}

/**
* {@inheritDoc }
*/
@Override
protected void newGame() {
int set = PreferencesService.instance().getIconsSet();
mMemory = new Memory( icons_set[ set ], sounds , not_found_tile_set[ set ], this);
mMemory.reset();
mGridView = (MemoryGridView) findViewById(R.id.gridview);
mGridView.setMemory(mMemory);
drawGrid();
}

/**
* {@inheritDoc }
*/
@Override
protected void preferences() {
Intent intent = new Intent(this, PreferencesActivity.class);
startActivity(intent);
}

/**
* {@inheritDoc }
*/
@Override
protected void onResume() {
super.onResume();

mMemory.onResume(PreferencesService.instance().getPrefs());

drawGrid();

}

/**
* {@inheritDoc }
*/
@Override
protected void onPause() {
super.onPause();

mMemory.onPause(PreferencesService.instance().getPrefs(), mQuit);

}

/**
* {@inheritDoc }
*/
public void onComplete(int countMove) {
int nHighScore = PreferencesService.instance().getHiScore();
String title = getString(R.string.success_title);
Object[] args = { countMove, nHighScore };
String message = MessageFormat
.format(getString(R.string.success), args);
int icon = R.drawable.win;
if (countMove < nHighScore) {
title = getString(R.string.hiscore_title);
message = MessageFormat.format(getString(R.string.hiscore), args);
icon = R.drawable.hiscore;

PreferencesService.instance().saveHiScore(countMove);
}
this.showEndDialog(title, message, icon);
}

/**
* {@inheritDoc }
*/
public void onUpdateView() {
drawGrid();
}

/**
* Draw or redraw the grid
*/
private void drawGrid() {
mGridView.update();
}

}

下面是显示错误的 Logcat。

06-03 14:54:17.095: E/Trace(19172): error opening trace file: No such file or directory (2) 06-03 14:54:17.392: E/AndroidRuntime(19172): FATAL EXCEPTION: main 06-03 14:54:17.392: E/AndroidRuntime(19172): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyt.game.play.fun/com.nyt.game.play.fun.ui.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=2; index=3 06-03 14:54:17.392: E/AndroidRuntime(19172):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351) 06-03 14:54:17.392: E/AndroidRuntime(19172):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403) 06-03 14:54:17.392: E/AndroidRuntime(19172):   at android.app.ActivityThread.access$600(ActivityThread.java:165) 06-03 14:54:17.392: E/AndroidRuntime(19172):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 06-03 14:54:17.392: E/AndroidRuntime(19172):    at android.os.Handler.dispatchMessage(Handler.java:107) 06-03 14:54:17.392: E/AndroidRuntime(19172):    at android.os.Looper.loop(Looper.java:194) 06-03 14:54:17.392: E/AndroidRuntime(19172):     at android.app.ActivityThread.main(ActivityThread.java:5370) 06-03 14:54:17.392: E/AndroidRuntime(19172):   at java.lang.reflect.Method.invokeNative(Native Method) 06-03 14:54:17.392: E/AndroidRuntime(19172):    at java.lang.reflect.Method.invoke(Method.java:525) 06-03 14:54:17.392: E/AndroidRuntime(19172):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 06-03 14:54:17.392: E/AndroidRuntime(19172):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 06-03 14:54:17.392: E/AndroidRuntime(19172):    at dalvik.system.NativeStart.main(Native Method) 06-03 14:54:17.392: E/AndroidRuntime(19172): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=2; index=3 06-03 14:54:17.392: E/AndroidRuntime(19172):   at com.nyt.game.play.fun.ui.MainActivity.newGame(MainActivity.java:72) 06-03 14:54:17.392: E/AndroidRuntime(19172):     at com.nyt.game.play.fun.ui.MainActivity.onCreate(MainActivity.java:52) 06-03 14:54:17.392: E/AndroidRuntime(19172):    at android.app.Activity.performCreate(Activity.java:5122) 06-03 14:54:17.392: E/AndroidRuntime(19172):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150) 06-03 14:54:17.392: E/AndroidRuntime(19172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)

下面是icons_set

private static final int[][] icons_set = { tiles_fruits, tiles_foods };

这是 getIconsSet 方法

public int getIconsSet() {
return getPrefs().getInt(PREF_ICONS_SET, ICONS_SET_FRUITS);

}

完整代码在这里

Code is here

最佳答案

你得到一个java.lang.ArrayIndexOutOfBoundsException: length=2;索引=3

因为 int set = PreferencesService.instance().getIconsSet(); 返回 3,而您的数组 icons_set 只有两个参数。

您确保数组有足够的元素。

关于java - Android 代码错误导致应用程序意外停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30616832/

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