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

问题描述

当我在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);之后

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...