应用程序在启动新意图时崩溃

时间:2022-02-02 23:13:53

Its been a while since I made an app. So I may be forgetting something. Whenever I press a button to start another intent, the app crashes. Is there something I'm missing?

我做了一个应用程序已经有一段时间了。所以我可能会忘记一些事情。每当我按下按钮开始另一个意图时,应用程序崩溃。有什么我想念的吗?

 public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        Button playButton = findViewById(R.id.play_button);
        playButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent play = new Intent(MainActivity.this, PointsActivity.class);
                startActivity(play);
            }
        });

        Button leaderButton = findViewById(R.id.leader_board);
        leaderButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent leader = new Intent(MainActivity.this, LeaderBoard.class);
                startActivity(leader);
            }
        });


    }


}

2 个解决方案

#1


1  

Look like you are not declaring your next activity in AndroidMainfest.xml

看起来您没有在AndroidMainfest.xml中声明您的下一个活动

<manifest ... >
  <application ... >
      <activity android:name=".PointsActivity" />
      <activity android:name=".LeaderBoard" />
      ...
  </application ... >
  ...
</manifest >

#2


0  

Without your stacktrace it is impossible to know what your error is, are you getting invalid casting exceptions? Usually you need to cast widgets into objects before assigning them to variables Ie your line

没有你的堆栈跟踪,你不可能知道你的错误是什么,你得到无效的转换异常吗?通常,您需要将小部件转换为对象,然后再将它们分配给变量即行

Button playButton = findViewById(R.id.playbutton);

按钮playButton = findViewById(R.id.playbutton);

should be

Button playButton = (Button)findViewById(R.id.playbutton);

按钮playButton =(Button)findViewById(R.id.playbutton);

notice the explicit cast to Button, you may be getting errors like cannot cast android.widget.button to button, this will fix those.

注意显式转换为Button,你可能会收到错误,如无法将android.widget.button转换为按钮,这将解决这些问题。

#1


1  

Look like you are not declaring your next activity in AndroidMainfest.xml

看起来您没有在AndroidMainfest.xml中声明您的下一个活动

<manifest ... >
  <application ... >
      <activity android:name=".PointsActivity" />
      <activity android:name=".LeaderBoard" />
      ...
  </application ... >
  ...
</manifest >

#2


0  

Without your stacktrace it is impossible to know what your error is, are you getting invalid casting exceptions? Usually you need to cast widgets into objects before assigning them to variables Ie your line

没有你的堆栈跟踪,你不可能知道你的错误是什么,你得到无效的转换异常吗?通常,您需要将小部件转换为对象,然后再将它们分配给变量即行

Button playButton = findViewById(R.id.playbutton);

按钮playButton = findViewById(R.id.playbutton);

should be

Button playButton = (Button)findViewById(R.id.playbutton);

按钮playButton =(Button)findViewById(R.id.playbutton);

notice the explicit cast to Button, you may be getting errors like cannot cast android.widget.button to button, this will fix those.

注意显式转换为Button,你可能会收到错误,如无法将android.widget.button转换为按钮,这将解决这些问题。