Android应用在切换活动时崩溃。

huangapple go评论98阅读模式
英文:

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."

  1. public class EncounterScreen extends AppCompatActivity implements View.OnClickListener {
  2. Player player=new Player();
  3. TextView healthText=findViewById(R.id.healthText);
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_encounter_screen);
  8. Player player=new Player();
  9. TextView healthText=findViewById(R.id.healthText);
  10. healthText.setText("Health "+player.getCurrentHP());
  11. Button attackButton=findViewById(R.id.attackBtn);
  12. Button drinkPotButton=findViewById(R.id.drinkPotBtn);
  13. Button runButton=findViewById(R.id.runBtn);
  14. attackButton.setOnClickListener(this);
  15. drinkPotButton.setOnClickListener(this);
  16. runButton.setOnClickListener(this);
  17. }
  18. @SuppressLint("SetTextI18n")
  19. @Override
  20. public void onClick(View view) {
  21. switch (view.getId()){
  22. case R.id.drinkPotBtn:
  23. player.drinkPotion(player);
  24. healthText.setText("Health "+player.getCurrentHP());
  25. break;
  26. }
  27. }
  28. }

Image description

英文:

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"

  1. public class EncounterScreen extends AppCompatActivity implements View.OnClickListener {
  2. Player player=new Player();
  3. TextView healthText=findViewById(R.id.healthText);
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_encounter_screen);
  8. Player player=new Player();
  9. TextView healthText=findViewById(R.id.healthText);
  10. healthText.setText("Health "+player.getCurrentHP());
  11. Button attackButton=findViewById(R.id.attackBtn);
  12. Button drinkPotButton=findViewById(R.id.drinkPotBtn);
  13. Button runButton=findViewById(R.id.runBtn);
  14. attackButton.setOnClickListener(this);
  15. drinkPotButton.setOnClickListener(this);
  16. runButton.setOnClickListener(this);
  17. }
  18. @SuppressLint("SetTextI18n")
  19. @Override
  20. public void onClick(View view) {
  21. switch (view.getId()){
  22. case R.id.drinkPotBtn:
  23. player.drinkPotion(player);
  24. healthText.setText("Health "+player.getCurrentHP());
  25. break;
  26. }
  27. }

答案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

  1. Player player = new Player();
  2. TextView healthText = findViewById(R.id.healthText);
  3. 我认为问题出在上面的那行代码移除 **=findViewById(R.id.healthText)**
英文:
  1. Player player=new Player();
  2. TextView healthText=findViewById(R.id.healthText);

I believe the issue is with the above line. remove =findViewById(R.id.healthText).

huangapple
  • 本文由 发表于 2020年8月22日 01:33:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63527534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定