2012-06-26 146 views
1

我正在開發此測驗形式的簡單應用程序,其中用戶獲取問題和4個選項。這些問題正在從SQLite DB中獲取。在我的succ.java活動中,我顯示了這個問題,並在點擊下一個按鈕時獲取下一個問題。java.lang.IllegalStateException:無法執行活動的方法

現在,當它在仿真器上運行時,如果我不選擇任何4個單選按鈕,我的應用程序最終會停止,我必須強制關閉。爲了處理這個問題,我想檢查一下我的succ.java頁面,如果這兩個選項都沒有選擇從數據庫提取相同問題ID的問題,並且如果我選擇了下一個問題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); 
    } 
} 
} 

logcat的

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? – 2012-06-26 06:26:53

+0

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

回答

1

如果我不選擇任何一個4個單選按鈕我的應用程序最終停止 ,我必須強制關閉。

它按預期發生根據您的代碼。

你正在做的最大的錯誤是當你檢查它們時初始化你的單選按鈕。下面是你寫的一個片段。

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); 

因此,如果情況是,如果你不選擇任何單選按鈕既不將被初始化,然後,如果單擊下一步按鈕,你會明顯得到NPE。

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

Wooaa ..我通過了。謝謝!我從現在開始就一直在這個問題上。非常感謝。 – user1481789

+0

@ user1481789每當NPE發生時,它總是與大部分時間未初始化的東西有關。 –

+0

謝謝我剛剛開始用java和android開發。我會牢記這一點。謝謝一堆。 – user1481789

相關問題