英文:
Android app crashes when changing activity
问题
New to Android development, using Android Studio. The first activity of my app is a simple main menu with a "Start" and a "Quit" button. When I press the "Start" button, the app should take me to the second activity named "EncounterScreen". Instead, the app crashes.
I am trying to display the current health of the player object, of class Player. Apparently, the problem is with TextView healthText=findViewById(R.id.healthText);
. This is the error: "Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference."
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;
}
}
}
英文:
New to Android developing, using Android Studio. The first activity of my app is a simple main menu with a "Start" and a "Quit" button. When i press the "Start" button, the app should take me to the second activity named "EncounterScreen". Instead, the app crashes. enter image description here. I am trying to display the current health of the player object, of class Player. Apparently the problem is with "TextView healthText=findViewById(R.id.healthText);". This is the error: "Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference"
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;
}
}
答案1
得分: 0
请确保您正在尝试查找的 TextView 的 id 正确无误。您正在使用 "R.id.healthText"。
您的 activity_ecounter_screen 中的 TextView 是否有不同的 id?
英文:
Make sure the TextView id that you are trying to find has the correct id. You are using "R.id.healthText"
Does the TextView in your activity_ecounter_screen have a different id?
答案2
得分: -1
Player player = new Player();
TextView healthText = findViewById(R.id.healthText);
我认为问题出在上面的那行代码。移除 **=findViewById(R.id.healthText)**。
英文:
Player player=new Player();
TextView healthText=findViewById(R.id.healthText);
I believe the issue is with the above line. remove =findViewById(R.id.healthText).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论