Search code examples
javaandroidx86intelnosuchmethoderror

Android java.lang.NoSuchMethodError: , happen on Intel x86 only (or lower Android version 4.4, 5.0)


My android app throws NoSuchMethodError and it only happens on intel x86 platform. You can see the code on the line 744 and log below.

I guess it's because the compatibility on x86 about setSpan or getEditableText, but search to find no related hint. Does anyone have similar experiences?

Thanks in advance for any suggestion!

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)

By @Micho's suggestion, I split the statement to below,

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

and find there's a warning about getColor(). I am not sure whether it's the reason of java.lang.NoSuchMethodError(or the crash reports only happen on lower Android version 4.4, 5.0 . Trying to fix this warning first. Is there some alternative to replace the getColor()? Any suggestion is welcome!

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.

Solution

  • From the post below,it should be the deprecated getColor() method to cause. I modify as the suggestion to ContextCompat.getColor(context, R.color.your_color); and it does not happen this NoSuchMethodError yet.

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