尝试在空对象引用上调用虚方法 空对象引用上的虚方法
Attempt to invoke virtual method on a null object reference virtual method on a null object reference
当我 运行 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);
之后
当我 运行 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);