微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试在空对象引用上调用虚拟方法在空对象引用上调用虚拟方法

如何解决尝试在空对象引用上调用虚拟方法在空对象引用上调用虚拟方法

当我在android studio应用上运行此代码时出现问题。

打包com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class FirstScreen extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        TextView textView =  findViewById(R.id.newScreen);
        String value ;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_screen);
        Intent intent = getIntent();
        value=intent.getStringExtra("Sending");
        textView.setText(value);
    }
}

enter image description here

解决方法

TextView textView = findViewById(R.id.newScreen);应该在setContentView(R.layout.activity_first_screen);之后

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。