简体   繁体   English

为什么我的应用程序按钮无法使用java导航到android studio中的下一页

[英]why my app button isn't navigating to next page in android studio using java

I am new to app development, I am stuck on this following example in my book. 我是应用程序开发的新手,在本书中,我始终坚持下面的示例。

Can some one please help me that why my play button is not working? 有人可以帮我为什么我的播放按钮不起作用吗? After clicking on the play button the game should start and it should navigate to next activity. 单击播放按钮后,游戏应开始,并应导航到下一个活动。

Main page code 主页代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.packtpub.mathgamechapter3a.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="My Math Game"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dp"
    android:textSize="30sp" />

<ImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:id="@+id/imageView"
    android:src="@mipmap/ic_launcher"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="38dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play"
    android:id="@+id/buttonPlay"
    android:layout_marginTop="28dp"
    android:layout_below="@+id/imageView"
    android:layout_alignRight="@+id/button2"
    android:layout_alignEnd="@+id/button2"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignStart="@+id/button2" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="High Scores"
    android:id="@+id/button2"
    android:layout_below="@+id/buttonPlay"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Quit"
    android:id="@+id/button3"
    android:layout_below="@+id/button2"
    android:layout_alignRight="@+id/buttonPlay"
    android:layout_alignEnd="@+id/buttonPlay"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignStart="@+id/button2" />
</RelativeLayout>

Java code Java代码

package com.packtpub.mathgamechapter3a;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button buttonPlay = (Button)findViewById(R.id.buttonPlay);
    buttonPlay.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    Intent i;
    i = new Intent(this, GameActivity.class);
    startActivity(i);
}

}

Game page where it should navigate 应浏览的游戏页面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.packtpub.mathgamechapter3a.GameActivity">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="2"
    android:id="@+id/textPartA"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="24dp"
    android:textSize="70sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="x"
    android:id="@+id/textOperator"
    android:layout_alignTop="@+id/textPartA"
    android:layout_centerHorizontal="true"
    android:textSize="70sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="2"
    android:id="@+id/textPartB"
    android:layout_alignTop="@+id/textOperator"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textSize="70sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="="
    android:id="@+id/textView4"
    android:layout_below="@+id/textOperator"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="92dp"
    android:textSize="70sp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="2"
    android:id="@+id/buttonChoice1"
    android:layout_below="@+id/textView4"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="99dp"
    android:textSize="40sp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="4"
    android:id="@+id/buttonChoice2"
    android:layout_alignTop="@+id/buttonChoice1"
    android:layout_centerHorizontal="true"
    android:textSize="40sp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="6"
    android:id="@+id/buttonChoice3"
    android:layout_alignTop="@+id/buttonChoice2"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textSize="40sp" />
</RelativeLayout>

Java code Java代码

package com.packtpub.mathgamechapter3a;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class GameActivity extends Activity implements View.OnClickListener{

int correctAnswer;
Button buttonObjectChoice1;
Button buttonObjectChoice2;
Button buttonObjectChoice3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //The next line loads our UI design to the screen
    setContentView(R.layout.activity_game);

    //Here we initialize all our variables
    int partA = 9;
    int partB = 9;
    correctAnswer = partA * partB;
    int wrongAnswer1 = correctAnswer - 1;
    int wrongAnswer2 = correctAnswer + 1;

    /*Here we get a working object based on either the button
      or TextView class and base as well as link our new objects
      directly to the appropriate UI elements that we created previously*/

    TextView textObjectPartA = (TextView)findViewById(R.id.textPartA);
    TextView textObjectPartB = (TextView)findViewById(R.id.textPartB);
    buttonObjectChoice1 = (Button)findViewById(R.id.buttonChoice1);
    buttonObjectChoice2 = (Button)findViewById(R.id.buttonChoice2);
    buttonObjectChoice3 = (Button)findViewById(R.id.buttonChoice3);

    //Now we use the setText method of the class on our objects
    //to show our variable values on the UI elements.

    textObjectPartA.setText("" + partA);
    textObjectPartB.setText("" + partA);

    //which button receives which answer, at this stage is arbitrary.

    buttonObjectChoice1.setText("" + correctAnswer);
    buttonObjectChoice2.setText("" + wrongAnswer1);
    buttonObjectChoice3.setText("" + wrongAnswer2);

    buttonObjectChoice1.setOnClickListener(this);
    buttonObjectChoice2.setOnClickListener(this);
    buttonObjectChoice3.setOnClickListener(this);


}//onCreate ends here

@Override
public void onClick(View view) {
    //declare a new int to be used in all the cases
    int answerGiven=0;
    switch (view.getId()) {

        case R.id.buttonChoice1:
  //initialize a new int with the value contained in buttonObjectChoice1
            //Remember we put it there ourselves previously
         answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());

            //is it the right answer?
            if(answerGiven==correctAnswer) {//yay it's the right answer
               Toast.makeText(getApplicationContext(), 
"Well done!", Toast.LENGTH_LONG).show();
            }else{//uh oh!
                Toast.makeText(getApplicationContext(),
 "Sorry that's wrong", Toast.LENGTH_LONG).show();
            }
            break;

        case R.id.buttonChoice2:
            //same as previous case but using the next button
    answerGiven = Integer.parseInt("" + buttonObjectChoice2.getText());
            if(answerGiven==correctAnswer) {
                Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(),
 "Sorry that's wrong", Toast.LENGTH_LONG).show();
            }
            break;

        case R.id.buttonChoice3:
            //same as previous case but using the next button
      answerGiven = Integer.parseInt("" + buttonObjectChoice3.getText());
            if(answerGiven==correctAnswer) {
                Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(getApplicationContext(),"Sorry that's wrong",
Toast.LENGTH_LONG).show();
            }
            break;

    }

}

}

well you need to do a if statement on the being clicked on. 好,您需要对被点击的内容做一个if语句。

if(view.getId() == R.id.buttonPlay)
{
Intent intent = new Intent(getBaseContext(), GameActivity.class);
startActivity(intent);
}

also make sure game activity is registered in the manifest file. 还要确保游戏活动已在清单文件中注册。

i checked your code it is correct . 我检查了您的代码是否正确。

I think you forget to register your activity in manifests 我认为您忘记在清单中注册活动了

please check that 请检查

Intent in = getIntent();

is missing.. the 2nd activity should contain this line also add this 丢失..第二个活动应包含此行,并添加此行

if(view.getId() == R.id.buttonPlay)
{
Intent inte = new Intent(MainActivity.this, GameActivity.class);
startActivity(intent);
}

and do register in the manifest file 并在清单文件中注册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM