当Android应用程序到达“ArrayList <player> players = fs.getScores();”行时崩溃我无法弄清楚</player>

时间:2013-07-08 20:34:18

标签: java android onclick nullpointerexception onclicklistener

我一直在为Android开发游戏,我想把高分放入。我认为这段代码应该正常工作,并且无法理解为什么它不是。我已将其设置为按下菜单按钮时会打开一个对话框,供用户输入其名称。这可以工作但是当你按下确定时,应用程序崩溃了。它崩溃的行是ArrayList<Player> players= fs.getScores();

以下是活动类的代码,它崩溃的地方:

package com.example.game;
import java.util.Collections;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.widget.EditText;
import android.view.LayoutInflater;

public class MyGame extends Activity {

    GameView gv;
    private final int DIALOG_TEXT_ENTRY = 1;
    Filestore fs;
    private String GAME_KEY= "game_key";
    private Bundle sis;
    TextView hscores;
    String name = "";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        gv = new GameView(this);
        setContentView(gv);
    gv.setStarted(true);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
    if (keyCode==KeyEvent.KEYCODE_DPAD_RIGHT)
        gv.setSpriteState(2);
    if (keyCode==KeyEvent.KEYCODE_DPAD_LEFT)
        gv.setSpriteState(1);
    if (keyCode == KeyEvent.KEYCODE_MENU)
        showDialog(DIALOG_TEXT_ENTRY);
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu); 
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_game, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection
        switch (item.getItemId()) {
        case R.id.new_game:
            gv = new GameView(this);
            gv.setStarted(true);
            setContentView(gv);
            return true;
        case R.id.save:
            showDialog(DIALOG_TEXT_ENTRY);
            return true;
        case R.id.quit:
            finish();
            return true;
        case R.id.highscores:
            setContentView(R.layout.highscores);
            hscores = (TextView) findViewById(R.id.myhighscores);
            ArrayList<Player> players= fs.getScores();
            if(!(players==null))
            {
                Collections.sort(players);
                String s=String.format("%15s%15s","NAME","SCORE\n");
                for(Player p:players)
                {
                    s+=String.format("%15s%15s",p.getName(),
                                    p.getScore()+"\n");
                }
                hscores.setText(s);
            }
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_TEXT_ENTRY:
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.save, null);
        final EditText t = (EditText) textEntryView.findViewById(R.id.save_edit);
        name = t.toString();
        return new AlertDialog.Builder(MyGame.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_save_title)
                .setView(textEntryView)
                .setPositiveButton(R.string.alert_dialog_ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                Player p= new Player(t.getText().toString(),gv.getTime());
                                ArrayList<Player> players= fs.getScores();
                                if(players==null)
                                    players= new ArrayList<Player>();
                                players.add(p);
                                fs.saveScores(players);
                            }
                        })
                .setNegativeButton(R.string.alert_dialog_cancel,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {

                            }
                        }).create();
    }
    return null;
}

public void onClick(DialogInterface dialog,
        int whichButton) {
    Player p= new Player(name,gv.getTime());
    ArrayList<Player> players= fs.getScores();
    if(players==null)
            players= new ArrayList<Player>();
    players.add(p);
    fs.saveScores(players);
}

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putBundle(GAME_KEY, gv.saveState());
    sis=outState;
}

}

这是logcat:

07-08 20:56:24.799: E/AndroidRuntime(1649): FATAL EXCEPTION: main
07-08 20:56:24.799: E/AndroidRuntime(1649): java.lang.NullPointerException
07-08 20:56:24.799: E/AndroidRuntime(1649):     at com.example.game.MyGame$1.onClick(MyGame.java:117)
07-08 20:56:24.799: E/AndroidRuntime(1649):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
07-08 20:56:24.799: E/AndroidRuntime(1649):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 20:56:24.799: E/AndroidRuntime(1649):     at android.os.Looper.loop(Looper.java:123)
07-08 20:56:24.799: E/AndroidRuntime(1649):     at android.app.ActivityThread.main(ActivityThread.java:3687)
07-08 20:56:24.799: E/AndroidRuntime(1649):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 20:56:24.799: E/AndroidRuntime(1649):     at java.lang.reflect.Method.invoke(Method.java:507)
07-08 20:56:24.799: E/AndroidRuntime(1649):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-08 20:56:24.799: E/AndroidRuntime(1649):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-08 20:56:24.799: E/AndroidRuntime(1649):     at dalvik.system.NativeStart.main(Native Method)

我试图包括我能想到的一切,但我是新手。任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:1)

ArrayList<Player> players= fs.getScores();

似乎fs指向null并导致NullPointerException

在您的代码中,您无法将fs指向有效的实例/对象。

答案 1 :(得分:0)

字段fs(文件存储)未初始化。您需要先创建(或获取)实例,可能就像在GameView方法中初始化onCreate一样。