如何在 Android 中以编程方式将图标设置为警报对话框
How to set Icon to the Alert Dialog Programatically in Android
Em 使用这些语句创建警报对话框并为其设置图标,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(this.getDrawable(R.drawable.video_downloader));
builder.setTitle("Congratulations !!!");
builder.show();
但它在 setIcon 时崩溃,当我检查可绘制文件夹时,video_downloader 图标已经存在。
AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View.onClick(View.java:3607)
at android.view.View.performClick(View.java:4212)
at android.view.View$PerformClick.run(View.java:17476)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View.onClick(View.java:3602)
at android.view.View.performClick(View.java:4212)
at android.view.View$PerformClick.run(View.java:17476)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: **java.lang.NoSuchMethodError**: numan.downloader.Test.Front.getDrawable
at numan.downloader.Test.Front.GetMedia(Front.java:115)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View.onClick(View.java:3602)
at android.view.View.performClick(View.java:4212)
at android.view.View$PerformClick.run(View.java:17476)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
07-31 08:35:51.291 500-519/? E/AppErrorDialog: 无法获取 ILowStorageHandle 实例
使用这个
builder.setIcon(ContextCompat.getDrawable(MyActivity.this,R.drawable.video_downloader));
而不是这个
builder.setIcon(this.getDrawable(R.drawable.video_downloader));
仅供参考
getDrawable()
方法已在 API 级别 22 中弃用。
试试这个,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(getResources().getDrawable(R.drawable.video_downloader));
builder.setTitle("Congratulations !!!");
builder.show();
你的错误发生在这里builder.setIcon(this.getDrawable(R.drawable.video_downloader));
试试这个
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.video_downloader);
参考this
有两种方法可以设置警告对话框中的图标
1) AlertDialog.Builder setIcon(Drawable icon)
设置要在标题中使用的 Drawable。
用法: builder.setIcon(ContextCompat.getDrawable(上下文,R.drawable.video_downloader));
2) AlertDialog.Builder setIcon(int iconId)
设置要在标题中使用的Drawable的资源id。
用法: builder.setIcon(R.drawable.video_downloader);
Em 使用这些语句创建警报对话框并为其设置图标,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(this.getDrawable(R.drawable.video_downloader));
builder.setTitle("Congratulations !!!");
builder.show();
但它在 setIcon 时崩溃,当我检查可绘制文件夹时,video_downloader 图标已经存在。
AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View.onClick(View.java:3607)
at android.view.View.performClick(View.java:4212)
at android.view.View$PerformClick.run(View.java:17476)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View.onClick(View.java:3602)
at android.view.View.performClick(View.java:4212)
at android.view.View$PerformClick.run(View.java:17476)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: **java.lang.NoSuchMethodError**: numan.downloader.Test.Front.getDrawable
at numan.downloader.Test.Front.GetMedia(Front.java:115)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.view.View.onClick(View.java:3602)
at android.view.View.performClick(View.java:4212)
at android.view.View$PerformClick.run(View.java:17476)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
07-31 08:35:51.291 500-519/? E/AppErrorDialog: 无法获取 ILowStorageHandle 实例
使用这个
builder.setIcon(ContextCompat.getDrawable(MyActivity.this,R.drawable.video_downloader));
而不是这个
builder.setIcon(this.getDrawable(R.drawable.video_downloader));
仅供参考
getDrawable()
方法已在 API 级别 22 中弃用。
试试这个,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(getResources().getDrawable(R.drawable.video_downloader));
builder.setTitle("Congratulations !!!");
builder.show();
你的错误发生在这里builder.setIcon(this.getDrawable(R.drawable.video_downloader));
试试这个
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.video_downloader);
参考this
有两种方法可以设置警告对话框中的图标
1) AlertDialog.Builder setIcon(Drawable icon)
设置要在标题中使用的 Drawable。
用法: builder.setIcon(ContextCompat.getDrawable(上下文,R.drawable.video_downloader));
2) AlertDialog.Builder setIcon(int iconId)
设置要在标题中使用的Drawable的资源id。
用法: builder.setIcon(R.drawable.video_downloader);