gpt4 book ai didi

AlertDialog 上的 Android RatingBar

转载 作者:行者123 更新时间:2023-11-29 14:05:54 24 4
gpt4 key购买 nike

当我运行下面的代码段时,我遇到了以下错误,该代码段在 AlertDialog 上显示了 RatingBar。错误是

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

我尝试注释掉“rB.setOnRatingBarChangeListener(new 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

最佳答案

不要设置 RatingBarChangeListener。设置 OndismissListener 或覆盖访问评级栏当前值的 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);
}
});

关于AlertDialog 上的 Android RatingBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7063642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com