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

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

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;
        }
    }
}

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"

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

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:

确定