2017-03-05 62 views
0

我是新来的应用程序开发,我在我的书中坚持下面这个例子。为什么我的应用程序按钮没有导航到Android工作室使用java的下一页

有人可以帮我,为什么我的播放按钮不工作?点击播放按钮后,游戏应该开始并且应该导航到下一个活动。

主页代码

<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代码

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

} 

游戏页面,它应该导航

<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代码

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; 

    } 

} 

} 

回答

0

我检查了你的代码,它是正确的。

我想你忘记舱单注册您的活动

请检查

+0

非常感谢你的帮助,它真的帮助。 – Zaman

0

那么你需要做被点击了一个if语句。

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

也确保游戏活动注册在清单文件中。

+0

,应该取代你的逻辑在onClick – Remario

0
Intent in = getIntent(); 

缺少.. 第二活动应该包含这一行 还添加此

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

并在清单文件中做注册

相关问题