طارق الجافي طارق الجافي - 4 months ago 24
Java Question

Android open empty activity when starting new activity

I am trying to make a simple quiz app

The user can answer the question and press "next question" button until he reaches the final question. When he press the button then the Android should show a new

Activity
containing the result.

But the problem here. When I tried the app, it shows an empty
Activity
instead.

And that's my code:

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 : is fine

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>


So where the problem?

Thx in advance ..

Answer

In your Result.java change

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

Notice the o in onCreate. It should be in small case.