Android在开始新活动时打开空活动

时间:2015-01-22 13:41:15

标签: java android android-intent android-activity

我正在尝试制作一个简单的测验应用

用户可以回答问题并按下"下一个问题"按钮直到他到达最后的问题。当他按下按钮时,Android应显示包含结果的新Activity

但这里的问题。当我尝试使用该应用时,它会显示空的Activity

那是我的代码:

AndroidManifest.xml:

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Quiz"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

 <activity
        android:name=".Result"
        android:label="@string/app_name" >

        <intent-filter>
            <action android:name="MySecond" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>      

</application>

</manifest>

Quiz.java:

package com.example.quizapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class Quiz extends Activity {

TextView tv;
Button nxt ;
RadioGroup rg;
RadioButton r1 , r2 , r3 ;

String question [] = {"your name ?","your age?"};
String ans[] = {"Tareq","22"};
String opt []= {"moh","Tareq","khaled","22","21","20"};

int flag = 0 ; 
public static int marks , correct , wrong ;


 public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.quiz);

        tv=(TextView)findViewById(R.id.question);
        nxt=(Button)findViewById(R.id.button1);
        rg=(RadioGroup)findViewById(R.id.radioGroup1);
        r1=(RadioButton)findViewById(R.id.radio1);
        r2=(RadioButton)findViewById(R.id.radio2);
        r3=(RadioButton)findViewById(R.id.radio3);


        tv.setText(question[flag]);
        r1.setText(opt[0]);
        r2.setText(opt[1]);
        r3.setText(opt[2]);

        nxt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                RadioButton selectedAns = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
                String selectedAnsText = selectedAns.getText().toString();

                if (selectedAnsText.equals(ans[flag])){

                    correct ++ ;
                }
                else {
                    wrong ++ ; 
                }
                flag ++ ;

                if ( flag < question.length)
                {
                    tv.setText(question[flag]);
                    r1.setText(opt[flag*3]);
                    r2.setText(opt[flag*3]+1);
                    r3.setText(opt[flag*3]+2);

                }
                else {

                    marks = correct - wrong ;
                    startActivity (new Intent("MySecond"));
                }

            }
        });

    }
}

Result.java:

 package com.example.quizapp;

 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.TextView;

 public class Result extends Activity{

TextView tv ; 


public void OnCreate(Bundle b){

    super.onCreate(b);
    setContentView(R.layout.result);


    tv = (TextView) findViewById(R.id.result);
    tv.setText("your final result is :");
}

}

quiz.xml:很好

result.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

<TextView
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

  </LinearLayout>

那么问题出在哪里?

提前......

1 个答案:

答案 0 :(得分:3)

Result.java更改

public void OnCreate(Bundle b){public void onCreate(Bundle b){

请注意o中的onCreate。它应该是小的。