重新启动应用程序时活动崩溃

时间:2018-01-28 20:10:09

标签: java android

我有两个活动可以说是水平活动和结果活动,所以当我从水平开始时会发生什么 - > level1 - >结果活动我检查分数是否> 4如果是,我通过在结果活动中执行类似的操作,在级别活动中删除按钮上的图像视图来解锁级别活动中的下一级别

SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        final int score111 = pref.getInt("score111", 0);
        v1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ResultActivity2.this,Lyricswho.class));
                if (score111 >= 4) {                       
                    Intent act2= new Intent(ResultActivity2.this,Lyricswho.class);
                    act2.putExtra("hideImageView", true);
                    startActivity(act2);
                }
            }
        });
    }

然后在Level活动中

if(score111 >=4 ){    
            hideImageView = getIntent().getExtras().getBoolean("hideImageView");
            if (hideImageView) {
                v.setVisibility(View.INVISIBLE);
            }
        }

第一次运行正常但是当我通过关闭它重新启动应用程序并转到Level Activity时它会崩溃并返回到之前的活动。我弄清楚的原因是当我重新启动应用程序时,hideImageView给出了空指针错误,因为它满足得分大于4的条件(之前它没有因为得分为0所以代码没有#&#t; t 39; t if if if condition)但是hideImageView中没有任何内容,所以它抛出null异常。我尝试使用布尔变量,以便如果条件只运行一次,但我的imageview再次出现,所以我不能使用它。我怎么解决这个问题?被困在那里好几天了。

1 个答案:

答案 0 :(得分:1)

我不明白应用崩溃时您指的是哪个活动。但是我看到了一些简单的修复。

首先,>>> from pprint import pprint >>> pprint(flat_pairs) [[(0, 1), (0, 5), (2, 9), (6, 9)], [(0, 1), (0, 5), (2, 9), (3, 9)], [(0, 1), (0, 5), (2, 9), (0, 9)], [(0, 1), (0, 5), (6, 9), (3, 9)], [(0, 1), (0, 5), (6, 9), (0, 9)], [(0, 1), (0, 5), (3, 9), (0, 9)], [(0, 1), (0, 2), (2, 9), (6, 9)], [(0, 1), (0, 2), (2, 9), (3, 9)], [(0, 1), (0, 2), (2, 9), (0, 9)], [(0, 1), (0, 2), (6, 9), (3, 9)], [(0, 1), (0, 2), (6, 9), (0, 9)], [(0, 1), (0, 2), (3, 9), (0, 9)], [(0, 1), (0, 9), (2, 9), (6, 9)], [(0, 1), (0, 9), (2, 9), (3, 9)], [(0, 1), (0, 9), (2, 9), (0, 9)], [(0, 1), (0, 9), (6, 9), (3, 9)], [(0, 1), (0, 9), (6, 9), (0, 9)], [(0, 1), (0, 9), (3, 9), (0, 9)], [(0, 5), (0, 2), (2, 9), (6, 9)], [(0, 5), (0, 2), (2, 9), (3, 9)], [(0, 5), (0, 2), (2, 9), (0, 9)], [(0, 5), (0, 2), (6, 9), (3, 9)], [(0, 5), (0, 2), (6, 9), (0, 9)], [(0, 5), (0, 2), (3, 9), (0, 9)], [(0, 5), (0, 9), (2, 9), (6, 9)], [(0, 5), (0, 9), (2, 9), (3, 9)], [(0, 5), (0, 9), (2, 9), (0, 9)], [(0, 5), (0, 9), (6, 9), (3, 9)], [(0, 5), (0, 9), (6, 9), (0, 9)], [(0, 5), (0, 9), (3, 9), (0, 9)], [(0, 2), (0, 9), (2, 9), (6, 9)], [(0, 2), (0, 9), (2, 9), (3, 9)], [(0, 2), (0, 9), (2, 9), (0, 9)], [(0, 2), (0, 9), (6, 9), (3, 9)], [(0, 2), (0, 9), (6, 9), (0, 9)], [(0, 2), (0, 9), (3, 9), (0, 9)]] 只有在hideImageView大于3时才会为真。您已经知道了。因此,只需将score111设置为v

其次,

  

hideImageView给出空指针错误

如果你在这里得到一个NullPointerException:

INVISIBLE

然后Intent发生了变化,hideImageView = getIntent().getExtras().getBoolean("hideImageView"); getIntent()返回null。只需检查这些事情都没有发生过。

getExtras()

但你再也不需要检查Intent intent = getIntent(); if (intent != null && intent.getExtras() != null) { // Now check hideImageView