I hit the following error when i run the code segment below which is showing a RatingBar
on an AlertDialog
. The error is
java.lang.IllegalStateException: Could not execute method of the activity
I try comment out "rB.setOnRatingBarChangeListener(new OnRatingBarChangeListener()...." portion and my rating bar can display out nicely at alertdialog. Any help is really appreciated. Many many thanks
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();
}
The logcat error is:
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
Don't set the RatingBarChangeListener
. Set the OndismissListener or Override the OnDismiss(Dialog) where you access the rating bar's current value.
Sample code:
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);
}
});
精彩评论