【问题标题】:How to use setCompoundDrawables with Xamarin c#如何在 Xamarin c# 中使用 setCompoundDrawables
【发布时间】:2015-03-17 15:50:16
【问题描述】:

所以很长一段时间以来,我一直在为特定的代码行苦苦挣扎。这里是:

btnForward.SetCompoundDrawablesWithIntrinsicBounds(0,0,Android.Resource.Drawable.IcMediaPlay,0);

每当调用此行时,我的应用程序就会崩溃,并且我的应用程序输出会显示以下内容:

[mono-rt] java.lang.NoSuchMethodError: no method with name='getDrawable' signature='(I)Landroid/graphics/drawable/Drawable;'在 Landroid/content/Context 类中;
[mono-rt] at com.enterprise.project.CallDetails.n_onCreate(Native Method)
[mono-rt] 在 com.enterprise.project.CallDetails.onCreate(CallDetails.java:29)
[mono-rt] 在 android.app.Activity.performCreate(Activity.java:5104)
[mono-rt] 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
[mono-rt] 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
[mono-rt] 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
[mono-rt] 在 android.app.ActivityThread.access$600(ActivityThread.java:141)
[mono-rt] 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
[mono-rt] 在 android.os.Handler.dispatchMessage(Handler.java:99)
[mono-rt] 在 android.os.Looper.loop(Looper.java:137)
[mono-rt] 在 android.app.ActivityThread.main(ActivityThread.java:5041)
[mono-rt] 在 java.lang.reflect.Method.invokeNative(Native Method)
[mono-rt] 在 java.lang.reflect.Method.invoke(Method.java:511)
[mono-rt] 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
[mono-rt] 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
[mono-rt] at dalvik.system.NativeStart.main(Native Method)

我尝试了 SetCompoundDrawables 的多种变体(intrinsicbound、relative 等),但由于许多其他线程说 WithIntrinsicBounds 是我要走的路。

我尝试使用 SetCompoundDrawablesWithIntrinsicBounds 的其他变体(使用 null 代替 0 和 Drawable 代替 ID),但出现了同样的问题。

如果可能是问题,这里是错误之前块中使用的几行代码

        case CallStatus.Paused: //Started but paused
            btnForward.Text = "Complete";
            btnForward.Clickable = true;
            btnForward.Visibility = ViewStates.Visible;
            btnForward.SetBackgroundColor (Android.Graphics.Color.Rgb (30, 118, 121));
            btnForward.SetCompoundDrawablesWithIntrinsicBounds(0,0,Android.Resource.Drawable.IcMediaPlay,0);
            break;

【问题讨论】:

  • 您了解 API 的级别吗?
  • Xamarin?安卓?我的 Android 目标是 14。我应该升级它吗?

标签: c# android xamarin


【解决方案1】:

在你的活动中试试这个:

首先检索资源id:

_flagResourceId = this.Resources.GetIdentifier ("IcMediaPlay", "drawable", this.PackageName);

然后设置drawable:

btnForward.SetCompoundDrawablesWithIntrinsicBounds(0,0,Resources.GetDrawable(_flagResourceId),0); Resources.GetDrawable (_flagResourceId)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2018-04-07
    • 2019-11-08
    • 2014-04-10
    • 1970-01-01
    相关资源
    最近更新 更多