Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android应用程序在更改活动时崩溃_Java_Android_Nullpointerexception - Fatal编程技术网

Java Android应用程序在更改活动时崩溃

Java Android应用程序在更改活动时崩溃,java,android,nullpointerexception,Java,Android,Nullpointerexception,Android开发新手,使用Android Studio。我的应用程序的第一个活动是一个简单的主菜单,带有“开始”和“退出”按钮。当我按下“开始”按钮时,应用程序将带我进入第二个名为“遭遇屏幕”的活动。相反,应用程序崩溃了。我试图显示player对象的当前运行状况,即类player的运行状况。显然,问题在于“TextView healthText=findViewById(R.id.healthText);”。这是错误:“尝试在空对象引用上调用虚拟方法“android.content.pm.Ap

Android开发新手,使用Android Studio。我的应用程序的第一个活动是一个简单的主菜单,带有“开始”和“退出”按钮。当我按下“开始”按钮时,应用程序将带我进入第二个名为“遭遇屏幕”的活动。相反,应用程序崩溃了。我试图显示player对象的当前运行状况,即类player的运行状况。显然,问题在于“TextView healthText=findViewById(R.id.healthText);”。这是错误:“尝试在空对象引用上调用虚拟方法“android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()”


我相信问题出在这一点上。删除=findViewById(R.id.healthText)

确保您尝试查找的TextView id具有正确的id。您正在使用“R.id.healthText”


activity\u ecounter\u屏幕中的TextView是否具有不同的id?

请在onCreate和setContentView(R.layout.activity\u Conference\u屏幕)之前发布崩溃错误消息您无法初始化TextView;您现在可以在构造函数中执行此操作。在SetContentView删除该零件后,将其放置在onCreate中。对play对象执行了相同的操作。现在,它在不崩溃的情况下切换活动。但现在,当我点击“喝药水”按钮,它应该增加健康值,并显示在屏幕上的变化,它崩溃了。现在它说:java.lang.NullPointerException:尝试在com.example.adventuregamealpha.EncounterScreen.onClick(EncounterScreen.java:42)上的空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'。它引用此行:healthText.setText(“Health”+player.getCurrentHP());删除findViewById只会创建一个新的bug,这不是一个可接受的答案我相信在调用onCreate方法之前从语句中删除findViewById不会创建新bug@但是删除它并不是答案,移动它可能是一个解决方案,但是删除它会产生问题,因为OP希望稍后在视图引用上调用setText()@Charankumarth身份证是正确的,我查过了。
public class EncounterScreen extends AppCompatActivity implements View.OnClickListener {
  Player player=new Player();
  TextView healthText=findViewById(R.id.healthText); 


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_encounter_screen);
    Player player=new Player();
    TextView healthText=findViewById(R.id.healthText);
    healthText.setText("Health "+player.getCurrentHP());

    Button attackButton=findViewById(R.id.attackBtn);
    Button drinkPotButton=findViewById(R.id.drinkPotBtn);
    Button runButton=findViewById(R.id.runBtn);
    attackButton.setOnClickListener(this);
    drinkPotButton.setOnClickListener(this);
    runButton.setOnClickListener(this);

}

@SuppressLint("SetTextI18n")
@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.drinkPotBtn:
            player.drinkPotion(player);
            healthText.setText("Health "+player.getCurrentHP());
        break;
        }

    }
Player player=new Player();
  TextView healthText=findViewById(R.id.healthText);