此处不支持空指针

时间:2019-02-05 17:05:41

标签: android nullpointerexception

profile.java

public class ProfileActivity extends AppCompatActivity {

    private TextView tvEmail;

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_profile );
        tvEmail = (TextView) findViewById(R.id.tvEmailProfile);
        tvEmail.setText(getIntent().getExtras().getString("Email"));
    }
}

1 个答案:

答案 0 :(得分:0)

可能您的意图中没有多余的名称“ Email”,您应该在尝试在TextView中进行设置之前进行检查:

public class ProfileActivity extends AppCompatActivity {

    private TextView tvEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_profile );
        tvEmail = (TextView) findViewById(R.id.tvEmailProfile);
        if(getIntent().hasExtra("Email")){
            tvEmail.setText(getIntent().getExtras().getString("Email"));
        }
    }
}

这将解决NullPointer问题。但是,您应该检查键“ Email”是否确实在找得到。