java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ImageView.setImageResource(int)' [英] java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

查看:74
本文介绍了java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ImageView.setImageResource(int)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ImageView.setImageResource(int)'

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

我想转移第一个活动的图标/图像或移到第二个活动,但是上面的错误显示出来.

I want to make the icon/image of the first activity transfer or move to the second activity but this error above show up.

这是我在第一幕中的源代码:

This is my source code in the first actvty:

 public void next (View view){
    Intent intent = new Intent(this, Products.class);
    intent.putExtra("Button ID", btnId);
    startActivity(intent);
}

然后在第二行动

 btnId = getIntent().getIntExtra("Button ID", btnId);
    setContentValues();
}

private void setContentValues() {
    ImageView titleImg = (ImageView) findViewById(R.id.imageView_result_title);
    View view = (View)findViewById(R.id.layout_goals_goals);

    switch (btnId) {
        case R.id.imageButton_goals_car:
            titleImg.setImageResource(R.drawable.car);
            break;
        case R.id.imageButton_goals_education:
            titleImg.setImageResource(R.drawable.education);
            break;
        case R.id.imageButton_goals_health:
            titleImg.setImageResource(R.drawable.health);
            break;
        case R.id.imageButton_goals_house:
            titleImg.setImageResource(R.drawable.house);
            break;
        case R.id.imageButton_goals_saving:
            titleImg.setImageResource(R.drawable.savings);
            break;
        case R.id.imageButton_goals_travel:
            titleImg.setImageResource(R.drawable.travel);
            break;
        default:
            break;
    }
}

推荐答案

变量titleImg可能为null.您应该调试该函数,以查看是否实际上是从setContentValues()的第一条语句中找到的.

The variable titleImg is possibly null. You should debug into the function to see if it's actually found from the first statement in setContentValues() or not.

这篇关于java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ImageView.setImageResource(int)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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