开发者

Unknown error with Android: 2.3.3 - error "application...stopped unexpectedly

Java code:

package wip.test.app1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.Random;



public class WIPTEST extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     while(true)
     {
        Random generate = new Random();
        int seed = generate.nextInt(4);
        //QUESTIONS
        String question[] = new String[4];
        question[0]="Who is the current president of the United States?";
        question[1]="Who is the current president of the United States?";
        question[2]="Who is the current president of the United States?";
        question[3]="Who is the current president of the United States?";
        //ANSWERS
        //CN: answer[corresponding question][question number]
        String answer[][] = new String[4][4];
        answer[0][0]="George W. Bush";
        answer[0][1]="Barrack Obama";
        answer[0][2]="Chuck Norris";
        answer[0][3]="George Washington";
        answer[1][0]="George W. Bush";
        answer[1][1]="Barrack Obama";
        answer[1][2]="Chuck Norris";
        answer[1][3]="George Washington";
        answer[2][0]="George W. Bush";
        answer[2][1]="Barrack Obama";
        answer[2][2]="Chuck Norris";
        answer[2][3]="George Washington";
        answer[3][0]="George W. Bush";
     开发者_C百科   answer[3][1]="Barrack Obama";
        answer[3][2]="Chuck Norris";
        answer[3][3]="George Washington";
        //CORRECT_ANSWERS
        int correct[] = new int[4];
        correct[0]=1;
        correct[1]=2;
        correct[2]=2;
        correct[3]=2;
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText(question[seed]);
        RadioButton a = (RadioButton) findViewById(R.id.a);
        RadioButton b = (RadioButton) findViewById(R.id.b);
        RadioButton c = (RadioButton) findViewById(R.id.c);
        RadioButton d = (RadioButton) findViewById(R.id.d);
        a.setText(answer[seed][0]);
        b.setText(answer[seed][1]);
        c.setText(answer[seed][2]);
        d.setText(answer[seed][3]);
        setContentView(R.layout.main);
     }
   }
}

Note: these are sample questions

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/tv"
    android:textSize="15sp"
    />
<RadioGroup>
<RadioButton
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/a"
/>
<RadioButton
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/b"
/>
<RadioButton
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/c"
/>
<RadioButton
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/d"
/>
</RadioGroup>
</LinearLayout>

Eclipse does not give any errors on the java code or on the xml but when i run it on the emulator (2.3.3) it gives an error

The application (process wip.test.app1) has stopped unexpectedly. Please try again.

I have tried multiple times including changing the code to be only in the one function and not in different functions. Also, I am somewhat unfamiliar with the Android SDK, but I am capable in both C++ and Java.


you should not be looping forever in onCreate creating and initializing all those objects. Also findViewById must be called after setting a view


At first glance I'd say it's because

setContentView(R.layout.main);

needs to be place before any call such as

TextView tv = (TextView) findViewById(R.id.tv);

So your code should be

setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(question[seed]);
RadioButton a = (RadioButton) findViewById(R.id.a);
RadioButton b = (RadioButton) findViewById(R.id.b);
RadioButton c = (RadioButton) findViewById(R.id.c);
RadioButton d = (RadioButton) findViewById(R.id.d);
a.setText(answer[seed][0]);
b.setText(answer[seed][1]);
c.setText(answer[seed][2]);
d.setText(answer[seed][3]);

There may be other things though...


You need to call setContentView(R.layout.main) before you make any findViewById() calls. Your activity has no views until you do that. It's a good practice to call it immediately after the call to super.onCreate().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜