Android RatingBar на AlertDialog - PullRequest
       3

Android RatingBar на AlertDialog

2 голосов
/ 15 августа 2011

Я сталкиваюсь со следующей ошибкой при запуске сегмента кода ниже, который показывает RatingBar на AlertDialog.Ошибка:

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

Я пытаюсь закомментировать часть "rB.setOnRatingBarChangeListener (new OnRatingBarChangeListener () ...."), и моя строка рейтинга может хорошо отображаться на alerttdialog. Любая помощь действительно ценится. Многие многиеспасибо

            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

1 Ответ

0 голосов
/ 15 августа 2011

Не устанавливайте RatingBarChangeListener. Установите OndismissListener или Override OnDismiss (Dialog), где вы получаете доступ к текущему значению строки рейтинга.

Пример кода:

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