应用程序在使用Explicit Intent时崩溃

时间:2017-05-28 13:54:06

标签: java android android-intent crash

我是Java编程的初学者。我正在使用Android Studio中的Explicit intents。我遇到了一个非常恼人的问题,每当我尝试使用intent启动 Quiz_activity 时,我的应用程序崩溃了。 Java代码和XML似乎都很好。以下是XML和Java:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@drawable/quiz_front"
android:layout_height="match_parent"
tools:context="com.example.shujah.games.Quiz_front">


<Button
    android:id="@+id/start_quiz_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="78dp"
    android:text=""
    android:background="@android:color/transparent"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/from_quiz_front_to_menu_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:background="@android:color/transparent"
    android:textColor="@color/colorAccent"
    android:layout_alignLeft="@+id/start_quiz_btn"
    android:layout_alignParentBottom="true"
    android:layout_alignStart="@+id/start_quiz_btn"
    android:text="BACK" />

  </RelativeLayout>

爪哇:

public class Quiz_front extends AppCompatActivity {
 Button start_quiz;
Button back_to_menu;
Intent i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz_front);


    start_quiz = (Button) findViewById(R.id.start_quiz_btn);
    start_quiz.setHighlightColor(Color.DKGRAY);
    start_quiz.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            i = new Intent(getApplicationContext(), Quiz_activity.class);
            startActivity(i);


        }
    });
    back_to_menu = (Button) findViewById(R.id.from_quiz_front_to_menu_btn);
    back_to_menu.setTextColor(Color.WHITE);
    back_to_menu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            i = new Intent(getApplicationContext(), MenuActivity.class);
            startActivity(i);

        }
      });
       }}

这是我想去的活动:

  public class Quiz_activity extends AppCompatActivity {
 TextView choice1;
 TextView choice2;
 TextView choice3;
 TextView choice4;
 TextView num_right;
 TextView num_left;
 TextView sign;
 TextView score;
 Random r;
 int scor;
 Intent ii;
 Button back_to_front;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz_activity);





    back_to_front.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ii = new Intent(getApplicationContext(), Quiz_front.class);
            startActivity(ii);

        }
    });

    choice1 = (TextView) findViewById(R.id.choice1_tv);
    choice2 = (TextView) findViewById(R.id.choice2_tv);
    choice3 = (TextView) findViewById(R.id.choice3_tv);
    choice4 = (TextView) findViewById(R.id.choice4_tv);
    num_left = (TextView) findViewById(R.id.left_tv);
    num_right = (TextView) findViewById(R.id.right_tv);
    score = (TextView) findViewById(R.id.score_tv);
    sign = (TextView) findViewById(R.id.sign_tv);
    r = new Random();

   /* choice1.setTextColor(Color.BLUE);
    choice2.setTextColor(Color.BLUE);
    choice3.setTextColor(Color.BLUE);
    choice4.setTextColor(Color.BLUE);
    num_right.setTextColor(Color.YELLOW);
    num_left.setTextColor(Color.YELLOW);
    sign.setTextColor(Color.YELLOW);*/

    }
 }

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mathquiz"
tools:context="com.example.shujah.games.Quiz_activity">


<TextView
    android:id="@+id/right_tv"
    android:layout_width="wrap_content"
    android:layout_height="50dp"

    android:layout_alignParentTop="true"
    android:layout_marginTop="75dp"
    android:text="TextView"
    android:textSize="16sp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<TextView
    android:id="@+id/left_tv"
    android:layout_height="50dp"
    android:layout_width="wrap_content"
    android:textSize="16sp"
    android:text="TextView"
    android:layout_alignBottom="@+id/right_tv"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:id="@+id/sign_tv"
    android:layout_width="100dp"
    android:layout_height="90dp"
    android:text="TextView"
    android:textSize="20sp"
    android:layout_centerHorizontal="true"
    android:layout_alignBottom="@+id/right_tv" />
<TextView
    android:id="@+id/score_tv"
    android:layout_width="70dp"
    android:layout_height="30dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:text="TextView" />

  <TextView
    android:id="@+id/choice1_tv"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_marginEnd="30dp"
    android:layout_marginRight="30dp"
    android:text="TextView"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/sign_tv"
    android:layout_toStartOf="@+id/sign_tv" />

  <TextView
    android:id="@+id/choice3_tv"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginBottom="64dp"
    android:text="TextView"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/choice1_tv"
    android:layout_alignStart="@+id/choice1_tv"
    android:layout_alignRight="@+id/choice1_tv"
    android:layout_alignEnd="@+id/choice1_tv" />

   <TextView
    android:id="@+id/choice2_tv"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:text="TextView"
    android:layout_alignBottom="@+id/choice1_tv"
    android:layout_alignLeft="@+id/choice4_tv"
    android:layout_alignStart="@+id/choice4_tv"
    android:layout_marginLeft="13dp"
    android:layout_marginStart="13dp" />

  <TextView
    android:id="@+id/choice4_tv"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:text="TextView"
    android:layout_toStartOf="@+id/right_tv"
    android:layout_toLeftOf="@+id/right_tv"
    android:layout_alignBottom="@+id/choice3_tv" />

  <Button
    android:id="@+id/back_to_quiz_front"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Back"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/choice1_tv"
    android:layout_toEndOf="@+id/choice1_tv" />

</RelativeLayout>

这是Android Manifest.XML:

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.shujah.games">

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MenuActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.example.shujah.games.RPSactivity" />
    <activity android:name="com.example.shujah.games.Quiz_activity" />
    <activity android:name="com.example.shujah.games.Quiz_front"></activity>
  </application>

 </manifest>

1 个答案:

答案 0 :(得分:0)

添加back_to_front= (Button) findViewById(R.id.back_to_quiz_front);

back_to_front.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ii = new Intent(getApplicationContext(), Quiz_front.class);
            startActivity(ii);

        }
    });