【问题标题】:Caused by: java.lang.NullPointerException: Attempt to invoke interface method on a null object reference原因:java.lang.NullPointerException:尝试在空对象引用上调用接口方法
【发布时间】:2015-03-23 14:47:12
【问题描述】:

我无法将高分提交到 android 排行榜。现在排行榜是空的,没有提交任何内容。我有一个需要提交的 int "oldScore"。现在我正在尝试一段代码来提交它,但活动在调用时崩溃。我的代码:

public class GameOver extends BaseGameActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private GoogleApiClient mGoogleApiClient;
    public static int score;

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

        mGoogleApiClient.connect();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Drive.API)
        .addScope(Drive.SCOPE_FILE)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();

        int newScore = GameOver.score;

        SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);

        int oldScore = prefs.getInt("key", 0);
        if (newScore > oldScore) {
            SharedPreferences.Editor edit = prefs.edit();
            edit.putInt("key", newScore);
            edit.commit();

            EditText HighScore = (EditText) findViewById(R.id.HighScoreT);
            HighScore.setText("" + newScore);
        } else {
            EditText HighScore = (EditText) findViewById(R.id.HighScoreT);
            HighScore.setText("" + oldScore);
            Games.Leaderboards.submitScoreImmediate(getApiClient(),String.valueOf(R.string.number_guesses_leaderboard), oldScore);
        }
    }

Logcat:

原因:java.lang.NullPointerException:尝试调用接口 方法'无效 com.google.android.gms.common.api.GoogleApiClient.connect()' 为空 对象引用

【问题讨论】:

    标签: java android android-studio nullpointerexception


    【解决方案1】:

    声明

    mGoogleApiClient.connect();
    

    应该在mGoogleApiClient被实例化之后出现

    【讨论】:

    • 当我这样做时,我会收到以下错误:“java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet.”
    【解决方案2】:

    你正在调用方法

    connect()
    

    在物体上

    mGoogleApiClient
    

    没有实例化它

    你需要先写这个,

      mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();  
    

    那么这个,

    mGoogleApiClient.connect();
    

    【讨论】:

    • 当我这样做时,我会收到以下错误:“java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet.”
    • 你使用的是google登录,你应该调用onStart()中的方法
    • 我刚刚创建了方法 onStart() 和 onStop() 并把 mGoogleApiClient.connect();在 OnStart 但我得到了同样的错误。
    • 当我删除“Games.Leaderboards.submitScoreImmediate(getApiClient(), String.valueOf(R.string.number_guesses_leaderboard), oldScore);”我的活动很好..所以这就是我的错误的来源。我应该如何更改它以提交分数?
    【解决方案3】:

    在您初始化mGoogleApiClient 引用之前之后您不能调用connect()。喜欢,

    // mGoogleApiClient.connect();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();       
    mGoogleApiClient.connect(); // <-- move to here.
    

    【讨论】:

    • 当我这样做时,我会收到以下错误:“java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.IllegalStateException: GoogleApiClient is not connected yet.”
    • 好的。听起来我们已经修复了您的 NullPointerException。你有没有对你的new question做过任何研究?
    【解决方案4】:

    试试这个过程: 在加载 contentview 之前分配 google 客户端:

    private GoogleApiClient mGoogleApiClient;
    public static int score;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Drive.API)
        .addScope(Drive.SCOPE_FILE)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
    
        setContentView(R.layout.activity_game_over);
    }
    

    添加覆盖 onStart 并连接谷歌客户端。

    @Override
         public void onStart() {
            super.onStart();
            mGoogleApiClient.connect();
        }
    
    @Override
    public void onStop() {
        super.onStop();
        mGoogleApiClient.disconnect();
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-25
      • 2016-04-23
      • 2018-02-05
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      相关资源
      最近更新 更多