3

I am getting crash on this activity when I run the app. This is not due to appcompat as you can see in the stack trace. This is native android ListView class whose method is not found while running the app.

The same app is running properly on other devices. The minSdkVersion = 13 target =23 max =23 and current device of crashing is API 17.

FATAL EXCEPTION: main
 java.lang.NoSuchMethodError: android.widget.ListView.setElevation
 at com.example.saloni.laughgurumarketingapp.ContentDetails.getValues(ContentDetails.java: 392)
 at com.example.saloni.laughgurumarketingapp.ContentDetails.onCreate(ContentDetails.java: 298)
 at android.app.Activity.performCreate(Activity.java: 5122)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1081)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2270)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2358)
 at android.app.ActivityThread.access$600(ActivityThread.java: 156)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1340)
 at android.os.Handler.dispatchMessage(Handler.java: 99)
 at android.os.Looper.loop(Looper.java: 153)
 at android.app.ActivityThread.main(ActivityThread.java: 5297)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java: 511)
 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)

In the code it's just a set elevation method on which it is crashing as you can see in the trace. Similarly it crashed last time on using one of the TextView class method. Why is this happening?

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
Sushobhit333
  • 33
  • 1
  • 7

2 Answers2

6

There is no setElevation() method in api 17. You need to only call this on api 21 and higher.

https://developer.android.com/reference/android/view/View.html#setElevation(float)

If you want to call this method on api 21 devices do this:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
    // Do something for lollipop and above versions
} else{
    // do something for phones running an SDK before lollipop
}
Shmuel
  • 3,916
  • 2
  • 27
  • 45
  • but if this is true then what is the use of providing the min sdk version? if the method is definitely gonna crash then why make it available to developers? – Sushobhit333 Mar 02 '16 at 13:48
  • take a look at this http://stackoverflow.com/a/3940823/1764080 You need to do that for this method call – Shmuel Mar 02 '16 at 18:17
2

You can use ViewCompat.setElevation and your app won't crash on pre-21 systems; however it won't have any effect on those older devices either.

cd1
  • 15,908
  • 12
  • 46
  • 47