从的NullPointerException getExtras() [英] NullPointerException from getExtras()

查看:272
本文介绍了从的NullPointerException getExtras()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的意图,从一个活动像这样的数据传输到另一个问题:

I'm creating an intent to transfer data from one activity to another like this :

Intent intent = new Intent(this, ActivityHighScore.class);
    intent.putExtra("USERNAME", username);
    intent.putExtra("PLAYERMOVES", playerMoves);

    this.startActivity(intent);

然后我想检查是否所有的数据存在作为活动开始,因为它可以从其他来源没有这个数据被设置启动。使用im这个语句:

Then i want to check if all of this data exists as the activity starts, as it can be started from other sources without this data being set. Im using this statement:

        Bundle bundle = getIntent().getExtras();

    if (!bundle.getString("USERNAME").equals(null) && bundle.getInt("PLAYERMOVES") != 0){
        String username = bundle.getString("USERNAME");
        int playerMoves = bundle.getInt("PLAYERMOVES");
        addHighScore(username, playerMoves);

    }   

但是,这将导致null pointerexception,我完全知道如何。我想我得到了对字符串和.equals()交手,但我认为它是...任何帮助将是很大的AP preciated。谢谢你。

But this causes a null pointerexception and I'm entirely sure how. I thought I was getting to grips with Strings and .equals(), but I think its that... Any help would be greatly appreciated. Thanks.

推荐答案

替换

Bundle bundle = getIntent().getExtras();

    if (!bundle.getString("USERNAME").equals(null) && bundle.getInt("PLAYERMOVES") != 0){
        String username = bundle.getString("USERNAME");
        int playerMoves = bundle.getInt("PLAYERMOVES");
        addHighScore(username, playerMoves);

    } 

 if (getIntent().getStringExtra("USERNAME") != null && (getIntent().getIntExtra("PLAYERMOVES", 0) != 0){
        String username = bundle.getString("USERNAME");
        int playerMoves = bundle.getInt("PLAYERMOVES");
        addHighScore(username, playerMoves);

  } 

这篇关于从的NullPointerException getExtras()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆