2012-06-26 2 views
1

나는 사용자가 질문과 4 가지 옵션을 얻는 퀴즈 형식으로이 간단한 응용 프로그램을 개발 중입니다. 이 질문은 SQLite DB에서 가져옵니다. 내 succ.java 활동에서 질문을 표시하고 다음 버튼을 클릭하면 다음 질문을 가져옵니다.java.lang.IllegalStateException : 활동의 메서드를 실행할 수 없습니다.

이제 에뮬레이터에서 실행할 때 4 개의 라디오 버튼 중 하나를 선택하지 않으면 내 앱이 궁극적으로 멈추고 강제 종료해야합니다. 이 문제를 해결하기 위해 succ.java 페이지를 체크하는 것을 생각해 보았습니다. 옵션 중 하나도 선택하지 않으면 동일한 질문 ID를 가진 db로부터 질문을 가져오고, 둘 중 하나가 다음 질문 ID를 사용하여 질문을 가져옵니다.

하지만 여전히 작동하지 않을 것입니다. 가능한 모든 작업을 시도했습니다. 내 succ.java를 첨부하고 있습니다. 또한 logcat 함께.

succ.java

public class succ extends Activity { 
private static String[] response = new String[10]; 

RadioButton op1,op2,op3,op4; 

private int token_page; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.mainquespage); 



    int qno=0; 
    String ques=""; 
    String ans1=""; 
    String ans2=""; 
    String ans3=""; 
    String ans4=""; 
    //int token=0; 
    Bundle extras=getIntent().getExtras(); 
    //token=extras.getInt("SESS_ID"); 
    ques=extras.getString("KEY_QUES"); 
    ans1=extras.getString("KEY_ANS1"); 
    ans2=extras.getString("KEY_ANS2"); 
    ans3=extras.getString("KEY_ANS3"); 
    ans4=extras.getString("KEY_ANS4"); 
    qno=extras.getInt("KEY_ID"); 
    token_page=qno+1; 
    TextView quesnumber=(TextView)findViewById(R.id.quesIDTEXT); 
    quesnumber.setText("QUESTION: "+ qno); 
    TextView quescont=(TextView)findViewById(R.id.quesContText); 
    quescont.setText(ques); 
    TextView opt1=(TextView)findViewById(R.id.op1Text); 
    opt1.setText(ans1); 
    TextView opt2=(TextView)findViewById(R.id.op2Text); 
    opt2.setText(ans2); 
    TextView opt3=(TextView)findViewById(R.id.op3Text); 
    opt3.setText(ans3); 
    TextView opt4=(TextView)findViewById(R.id.op4Text); 
    opt4.setText(ans4); 

} 
public void onRadioButtonClicked(View view) { 

    op1 = (RadioButton) findViewById(R.id.op1Text); 
    op2 = (RadioButton) findViewById(R.id.op2Text); 
    op3 = (RadioButton) findViewById(R.id.op3Text); 
    op4 = (RadioButton) findViewById(R.id.op4Text); 

    if(op1.isChecked()) 
    { 
     response[token_page-2]=op1.getText().toString(); 
    } 
    else if(op2.isChecked()){ 
     response[token_page-2]=op2.getText().toString(); 
    } 
    else if(op3.isChecked()){ 
     response[token_page-2]=op3.getText().toString(); 
    } 
    else if(op4.isChecked()){ 
     response[token_page-2]=op4.getText().toString(); 
    } 
    else 
     response[token_page-2]=""; 

    } 
public void nextClick(View view) 
{ 
    final Context context=this; 
    Question question=new Question(); 
    MyAdapter questionAdapter=new MyAdapter(this); 

      if(token_page!=11) 
      { 
      questionAdapter.open(); 
    if(op1.isChecked()||op2.isChecked()||op3.isChecked()||op4.isChecked()) 
    { 
     question=questionAdapter.showQues(token_page); 
    } 
    else 
    { 
     question=questionAdapter.showQues(token_page-1); 
    } 
//please note when i'm just writing: question=questionAdapter.showQues(token_page) //without checking for the selection of radio buttons i get the error i the end after the //last question is answered but when i'm checking it here and using if els then am getting //an error on the same screen. i'm stuck on this since forever. HELP !! 

    questionAdapter.close(); 

     int qno; 
    String ques; 
    String ans1; 
    String ans2; 
    String ans3; 
    String ans4; 
    qno=question.getQno(); 
    ques=question.getQues(); 
    ans1=question.getAns1(); 
    ans2=question.getAns2(); 
    ans3=question.getAns3(); 
    ans4=question.getAns4(); 

     Intent intent=new Intent(context, succ.class); 
     intent.putExtra("KEY_ID", qno); 
     intent.putExtra("KEY_QUES",ques); 
     intent.putExtra("KEY_ANS1",ans1); 
     intent.putExtra("KEY_ANS2",ans2); 
     intent.putExtra("KEY_ANS3",ans3); 
     intent.putExtra("KEY_ANS4",ans4); 

      startActivity(intent); 

    }//token_page 
    else 
    { 
     Toast.makeText(this, "FINISHED", Toast.LENGTH_LONG).show(); 
     Bundle b=new Bundle(); 
     b.putStringArray("ARRAY", response); 
     Intent i=new Intent(context, result.class); 
     i.putExtras(b); 
     startActivity(i); 
    } 
} 
} 

로그 캣

06-26 11:26:48.401: D/opendb(357): EXIST 
06-26 11:26:52.321: W/KeyCharacterMap(357): No keyboard for id 0 
06-26 11:26:52.321: W/KeyCharacterMap(357): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
06-26 11:26:53.781: D/dalvikvm(357): GC_EXTERNAL_ALLOC freed 122K, 51% free 2699K/5447K, external 2128K/2137K, paused 63ms 
06-26 11:26:59.923: W/KeyCharacterMap(357): No keyboard for id 0 
06-26 11:26:59.923: W/KeyCharacterMap(357): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
06-26 11:29:49.512: D/opendb(478): EXIST 
06-26 11:29:58.981: D/AndroidRuntime(478): Shutting down VM 
06-26 11:29:58.981: W/dalvikvm(478): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
06-26 11:29:59.011: E/AndroidRuntime(478): FATAL EXCEPTION: main 
06-26 11:29:59.011: E/AndroidRuntime(478): java.lang.IllegalStateException: Could not execute method of the activity 
06-26 11:29:59.011: E/AndroidRuntime(478): at android.view.View$1.onClick(View.java:2144) 
06-26 11:29:59.011: E/AndroidRuntime(478): at android.view.View.performClick(View.java:2485) 
06-26 11:29:59.011: E/AndroidRuntime(478): at android.view.View$PerformClick.run(View.java:9080) 
06-26 11:29:59.011: E/AndroidRuntime(478): at android.os.Handler.handleCallback(Handler.java:587) 
06-26 11:29:59.011: E/AndroidRuntime(478): at android.os.Handler.dispatchMessage(Handler.java:92) 
06-26 11:29:59.011: E/AndroidRuntime(478): at android.os.Looper.loop(Looper.java:123) 
06-26 11:29:59.011: E/AndroidRuntime(478): at android.app.ActivityThread.main(ActivityThread.java:3683) 
06-26 11:29:59.011: E/AndroidRuntime(478): at java.lang.reflect.Method.invokeNative(Native Method) 
06-26 11:29:59.011: E/AndroidRuntime(478): at java.lang.reflect.Method.invoke(Method.java:507) 
06-26 11:29:59.011: E/AndroidRuntime(478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
06-26 11:29:59.011: E/AndroidRuntime(478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
06-26 11:29:59.011: E/AndroidRuntime(478): at dalvik.system.NativeStart.main(Native Method) 
06-26 11:29:59.011: E/AndroidRuntime(478): Caused by: java.lang.reflect.InvocationTargetException 
06-26 11:29:59.011: E/AndroidRuntime(478): at java.lang.reflect.Method.invokeNative(Native Method) 
06-26 11:29:59.011: E/AndroidRuntime(478): at java.lang.reflect.Method.invoke(Method.java:507) 
06-26 11:29:59.011: E/AndroidRuntime(478): at android.view.View$1.onClick(View.java:2139) 
06-26 11:29:59.011: E/AndroidRuntime(478): ... 11 more 
06-26 11:29:59.011: E/AndroidRuntime(478): Caused by: java.lang.NullPointerException 
06-26 11:29:59.011: E/AndroidRuntime(478): at my.pkg.newquiz.succ.nextClick(succ.java:98) 
06-26 11:29:59.011: E/AndroidRuntime(478): ... 14 more 
06-26 11:30:02.821: I/Process(478): Sending signal. PID: 478 SIG: 9 
+1

succ.java 파일의 행 번호 98은 어느 것입니까? –

+0

if (op1.isChecked() || op2.isChecked() || op3.isChecked() || op4.isChecked()) – user1481789

답변

1

난 내 응용 프로그램은 궁극적으로 를 중지하고 4 개 라디오 버튼 중 하나를 선택 해달라고 내가 가까이 강제해야합니다.

코드에 따라 예상대로 진행되고 있습니다.

여러분이하고있는 가장 큰 실수는 여러분이 라디오 버튼을 체크했을 때 초기화하는 것입니다. 아래에 작성한 스 니펫이 있습니다. 당신이 라디오 버튼 중 하나를 선택 해달라고하지 어느 쪽도 초기화됩니다 그리고 당신은 분명 NPE를 얻을 것이다 클릭 다음 버튼을 경우 경우

public void onRadioButtonClicked(View view) { 

    op1 = (RadioButton) findViewById(R.id.op1Text); 
    op2 = (RadioButton) findViewById(R.id.op2Text); 
    op3 = (RadioButton) findViewById(R.id.op3Text); 
    op4 = (RadioButton) findViewById(R.id.op4Text); 

그래서 경우의 시나리오입니다.

if(op1.isChecked()||op2.isChecked()||op3.isChecked()||op4.isChecked()) 
    { 
     question=questionAdapter.showQues(token_page); 
    } 

그래서 해결책은 onCreate의 모든 라디오 버튼을 초기화하는 것입니다.

아래는 수정 된 스 니펫입니다.

public class succ extends Activity { 
private static String[] response = new String[10]; 

RadioButton op1,op2,op3,op4; 

private int token_page; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.mainquespage); 

    op1 = (RadioButton) findViewById(R.id.op1Text); 
    op2 = (RadioButton) findViewById(R.id.op2Text); 
    op3 = (RadioButton) findViewById(R.id.op3Text); 
    op4 = (RadioButton) findViewById(R.id.op4Text); 


    int qno=0; 
    String ques=""; 
    String ans1=""; 
    String ans2=""; 
    String ans3=""; 
    String ans4=""; 
    //int token=0; 
    Bundle extras=getIntent().getExtras(); 
    //token=extras.getInt("SESS_ID"); 
    ques=extras.getString("KEY_QUES"); 
    ans1=extras.getString("KEY_ANS1"); 
    ans2=extras.getString("KEY_ANS2"); 
    ans3=extras.getString("KEY_ANS3"); 
    ans4=extras.getString("KEY_ANS4"); 
    qno=extras.getInt("KEY_ID"); 
    token_page=qno+1; 
    TextView quesnumber=(TextView)findViewById(R.id.quesIDTEXT); 
    quesnumber.setText("QUESTION: "+ qno); 
    TextView quescont=(TextView)findViewById(R.id.quesContText); 
    quescont.setText(ques); 
    TextView opt1=(TextView)findViewById(R.id.op1Text); 
    opt1.setText(ans1); 
    TextView opt2=(TextView)findViewById(R.id.op2Text); 
    opt2.setText(ans2); 
    TextView opt3=(TextView)findViewById(R.id.op3Text); 
    opt3.setText(ans3); 
    TextView opt4=(TextView)findViewById(R.id.op4Text); 
    opt4.setText(ans4); 


} 
public void onRadioButtonClicked(View view) { 



    if(op1.isChecked()) 
    { 
     response[token_page-2]=op1.getText().toString(); 
    } 
    else if(op2.isChecked()){ 
     response[token_page-2]=op2.getText().toString(); 
    } 
    else if(op3.isChecked()){ 
     response[token_page-2]=op3.getText().toString(); 
    } 
    else if(op4.isChecked()){ 
     response[token_page-2]=op4.getText().toString(); 
    } 
    else 
     response[token_page-2]=""; 

    } 
public void nextClick(View view) 
{ 
    final Context context=this; 
    Question question=new Question(); 
    MyAdapter questionAdapter=new MyAdapter(this); 

      if(token_page!=11) 
      { 
      questionAdapter.open(); 
    if(op1.isChecked()||op2.isChecked()||op3.isChecked()||op4.isChecked()) 
    { 
     question=questionAdapter.showQues(token_page); 
    } 
    else 
    { 
     question=questionAdapter.showQues(token_page-1); 
    } 
//please note when i'm just writing: question=questionAdapter.showQues(token_page) //without checking for the selection of radio buttons i get the error i the end after the //last question is answered but when i'm checking it here and using if els then am getting //an error on the same screen. i'm stuck on this since forever. HELP !! 

    questionAdapter.close(); 

     int qno; 
    String ques; 
    String ans1; 
    String ans2; 
    String ans3; 
    String ans4; 
    qno=question.getQno(); 
    ques=question.getQues(); 
    ans1=question.getAns1(); 
    ans2=question.getAns2(); 
    ans3=question.getAns3(); 
    ans4=question.getAns4(); 

     Intent intent=new Intent(context, succ.class); 
     intent.putExtra("KEY_ID", qno); 
     intent.putExtra("KEY_QUES",ques); 
     intent.putExtra("KEY_ANS1",ans1); 
     intent.putExtra("KEY_ANS2",ans2); 
     intent.putExtra("KEY_ANS3",ans3); 
     intent.putExtra("KEY_ANS4",ans4); 

      startActivity(intent); 

    }//token_page 
    else 
    { 
     Toast.makeText(this, "FINISHED", Toast.LENGTH_LONG).show(); 
     Bundle b=new Bundle(); 
     b.putStringArray("ARRAY", response); 
     Intent i=new Intent(context, result.class); 
     i.putExtras(b); 
     startActivity(i); 
    } 
} 
} 
+0

우어아. 감사합니다! 나는 지금부터이 일을 해왔다. 정말 고맙습니다. – user1481789

+0

@ user1481789 NPE가 발생할 때마다 항상 초기화되지 않은 항목과 관련이 있습니다. –

+0

나는 java와 android로 개발하기 시작했다. 나는 이것을 명심해야한다. 무리 감사. – user1481789

관련 문제