Android java.lang.NoSuchMethodError:仅发生在Intel x86上(或更低的Android版本4.4,5.0)

时间:2017-06-01 06:43:01

标签: java android x86 intel nosuchmethoderror

我的Android应用程序抛出NoSuchMethodError,它只发生在intel x86平台上。您可以在744行看到代码并在下面记录。

我想这是因为x86上关于setSpangetEditableText的兼容性,但搜索时没有找到相关的提示。有没有人有类似的经历?

提前感谢任何建议!

textView2.getEditableText().setSpan(new ForegroundColorSpan(
getColor(R.color.colorMiddleBlue))
, nowLength
, endSelection
, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  java.lang.NoSuchMethodError: 
  at com.SouthernPacificOceanFisher.speaking.MainActivity.showSentenceSpoken(MainActivity.java:744)
  at com.SouthernPacificOceanFisher.speaking.MainActivity.processOneSentence(MainActivity.java:757)
  at com.SouthernPacificOceanFisher.speaking.MainActivity.initSentences(MainActivity.java:793)
  at com.SouthernPacificOceanFisher.speaking.MainActivity.initDirFile_Text(MainActivity.java:825)
  at com.SouthernPacificOceanFisher.speaking.MainActivity.onCreate(MainActivity.java:340)
  at android.app.Activity.performCreate(Activity.java:5541)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
  at android.app.ActivityThread.access$900(ActivityThread.java:172)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:146)
  at android.app.ActivityThread.main(ActivityThread.java:5653)
  at java.lang.reflect.Method.invokeNative(Native Method:0)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
  at dalvik.system.NativeStart.main(Native Method:0)

根据@ Micho的建议,我将声明分成以下内容,

    Editable editable = textView2.getEditableText();
    int color1 = getColor(R.color.colorMiddleBlue);
    ForegroundColorSpan FcolorSpan = new    ForegroundColorSpan(color1);
    editable.setSpan(FcolorSpan, nowLength, endSelection,      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

并发现有关于getColor()的警告。我不确定这是java.lang.NoSuchMethodError的原因(或者崩溃报告只发生在较低的Android版本4.4,5.0上。 尝试先修复此警告。有替代getColor()的替代方法吗? 欢迎任何建议!

Call requires API level 23 (current min is 15): 
android.content.Context#getColor less... (Ctrl+F1) 
This check scans through all the Android API calls in the application and 
warns about any calls that are not available on all versions targeted by 
this application (according to its minimum SDK attribute in the manifest).  
If you really want to use this API and don't need to support older 
devices just set the minSdkVersion in your build.gradle or 
AndroidManifest.xml files.  
If your code is deliberately accessing newer APIs, and you have ensured 
(e.g. with conditional execution) that this code will only ever be called 
on a supported platform, then you can annotate your class or method with 
the @TargetApi annotation specifying the local minimum SDK to apply, such 
as @TargetApi(11), such that this check considers 11 rather than your 
manifest file's minimum SDK as the required API level.  
If you are deliberately setting android: attributes in style definitions, 
make sure you place this in a values-vNN folder in order to avoid running 
into runtime conflicts on certain devices where manufacturers have added 
custom attributes whose ids conflict with the new ones on later 
platforms.  Similarly, you can use tools:targetApi="11" in an XML file to 
indicate that the element will only be inflated in an adequate context.

1 个答案:

答案 0 :(得分:0)

从下面的帖子中,它应该是不推荐使用的getColor()方法。 我修改为ContextCompat.getColor(context,R.color.your_color)的建议;并且它还没有发生这种NoSuchMethodError。

getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)