【问题标题】:Attempt to invoke virtual method on a null object reference virtual method on a null object reference [duplicate]尝试在空对象引用上调用空对象引用虚拟方法[重复]
【发布时间】:2020-10-12 23:06:06
【问题描述】:

当我在 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

【问题讨论】:

  • 您在设置内容之前调用了 findViewById 谷歌搜索 findViewById 返回 null

标签: android nullpointerexception invoke mobile-application virtual-functions


【解决方案1】:

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

【讨论】:

  • 感谢您的帮助,现在您的工作是正确的
猜你喜欢
  • 2023-03-13
  • 2023-03-25
  • 1970-01-01
  • 2016-07-30
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多