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

时间:2015-06-03 09:59:30

标签: java android eclipse

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

<?php
if(isset($_POST["login"])){

   $error = validate($_POST);

    if(empty($error) {

    .....//your further processing code
    }else{
     // code to show your error at appropriate place.
    }
}
function validate($dataArray){
    $validateArray  = $dataArray; // either asign to a new variable or you can use directly $dataArray
    $errors = array();
    if(empty($_POST['user']) or !preg_match("/^[a-zA-Z0-9 ]*$/", $_POST['user'])){
        $errors['user'] = "You can't leave a field empty and you can only use letters, numbers and space.";
    }
    if(empty($_POST['pass']) or !preg_match("/^[a-zA-Z0-9 ]*$/", $_POST['pass'])){
            $errors['pass'] = "You can't leave a field empty and you can only use letters, numbers and space.";
     }
     //......so on

    return $errors;
}
?>

以下是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();
    }

}

下面是icons_set

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)

这是getIconsSet方法

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

完整代码在这里

Code is here

2 个答案:

答案 0 :(得分:1)

你得到java.lang.ArrayIndexOutOfBoundsException: length=2; index=3

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

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

答案 1 :(得分:0)

通过添加另一个Icons_set解决了问题。 bcoz有2个索引为0和1,这导致其他方法出现问题。