2011-08-15 64 views
2

我打以下錯誤,當我運行代碼段下方,其示出了上AlertDialog一個RatingBar。該錯誤是的Android的RatingBar上AlertDialog

java.lang.IllegalStateException: Could not execute method of the activity 

我嘗試註釋掉「rB.setOnRatingBarChangeListener(新OnRatingBarChangeListener()......」部分,我的評價欄可以在alertdialog很好地顯示出來。任何幫助真的感謝很多很多的感謝

  AlertDialog.Builder builder; 
      AlertDialog alertDialog; 

      Context mContext = getApplicationContext(); 

      LayoutInflater inflater = (LayoutInflater) 

      mContext.getSystemService(LAYOUT_INFLATER_SERVICE); 

      View layout = inflater.inflate(R.layout.review_app, (ViewGroup) findViewById(R.id.layout_root)); 

      builder = new AlertDialog.Builder(this); 
      builder.setView(layout); 

      final RatingBar rB = (RatingBar)findViewById(R.id.Rating01); 
      final TextView ratingdefinition = (TextView) findViewById(R.id.ratingdefinition); 


      rB.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { 

       public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 

        final int numStars = ratingBar.getNumStars(); 
        Log.v("number of stars", " " + numStars); 
        switch(numStars) 
        { 
         case(1): 
         { 
          ratingdefinition.setText("Poor"); 
          break; 
         } 
         case(2): 
         { 
          ratingdefinition.setText("Below average"); 
          break; 
         } 
         case(3): 
         { 
          ratingdefinition.setText("Average"); 
          break; 
         } 
         case(4): 
         { 
          ratingdefinition.setText("Above average"); 
          break; 
         } 
         case(5): 
         { 
          ratingdefinition.setText("Excellent!"); 
          break; 
         }              
        } 

       } 
       }); 

      alertDialog = builder.create(); 
      alertDialog.show(); 
     } 

的logcat的錯誤是:

08-15 09:49:02.789: WARN/dalvikvm(747): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747): FATAL EXCEPTION: main 
08-15 09:49:02.859: ERROR/AndroidRuntime(747): java.lang.IllegalStateException: Could not execute method of the activity 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at android.view.View$1.onClick(View.java:2072) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at android.view.View.performClick(View.java:2408) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at android.view.View$PerformClick.run(View.java:8816) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at android.os.Handler.handleCallback(Handler.java:587) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at android.os.Handler.dispatchMessage(Handler.java:92) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at android.os.Looper.loop(Looper.java:123) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at dalvik.system.NativeStart.main(Native Method) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747): Caused by: java.lang.reflect.InvocationTargetException 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at com.Maxis.Mplanet.IndividualApp.clickHandler(IndividualApp.java:293) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at android.view.View$1.onClick(View.java:2067) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  ... 11 more 
08-15 09:49:02.859: ERROR/AndroidRuntime(747): Caused by: java.lang.NullPointerException: println needs a message 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at android.util.Log.println_native(Native Method) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  at android.util.Log.v(Log.java:101) 
08-15 09:49:02.859: ERROR/AndroidRuntime(747):  ... 15 more 
+0

你可以張貼的錯誤全logcat的輸出? – Pikaling

+0

薇薇,我已經添加了全logcat的錯誤如上 –

回答

0

不要設置RatingBarChangeListener設置OndismissListener或重寫OnDismiss(對話),其中u訪問。評級欄的當前價值。


示例代碼:

alertDialog.setOnDismissListener(new OnDismissListener(..) { 
    void onDismiss(DialogInterface dlg) { 
     // find the rating bar and get its value. 
     // hold the alert dlg view in activity class n use it here. the one u set using alertDialog.setView 
     mAlertDialogView.findViewbyid(R.id.ratingbar); 
    } 
}); 
+0

user777777如何通過多少顆用戶評級欄上選擇,如果我使用setOnDismissListener?示例如何獲取RatingBar的值? –

+0

我編輯了上面的代碼。保持對dlg視圖的引用並在onDismiss中使用它。 – Ronnie