Getting NoSuchMethodError Using TextView Methods

I get a NoSuchMethodError with the class I created that extends TextView . The only thing I did was add some variables and add onLongClickListener . No more changes.

Everything works fine when I use my application in my Android phone 4.1.2 But in my other phone which is 4.0.3, it throws this NoSuchMethodError .

Here is the code when I create a class that extends Textview:

 descrip=new TextViewList(context, admin, this); descrip.setPadding(0, 15, 0, 15); descrip.setGravity(Gravity.CENTER); descrip.setTextAlignment(Gravity.CENTER); descrip.setText(c.getString(c.getColumnIndex("Descripcion"))); descrip.setTag(c.getString(c.getColumnIndex("ID"))); descrip.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); descrip.setHeight(LinearLayout.LayoutParams.MATCH_PARENT); descrip.setBackground(img); layDescripcion.addView(descrip); 

First, he threw an exception using setTextAlignment , then I removed it and threw it again using the setBackground method.

What causes this error? Does this mean that my application is not compatible with Android version lower than 4.1.2? I set minimun to 2.2 when I created the project. And I use the android.support.v4 libraries where they are requested.

Here is the LogCat:

 07-09 21:45:26.715: E/AndroidRuntime(13481): FATAL EXCEPTION: main 07-09 21:45:26.715: E/AndroidRuntime(13481): java.lang.NoSuchMethodError: modelo.TextViewList.setBackground 07-09 21:45:26.715: E/AndroidRuntime(13481): at modelo.ListaTextViewList.mostrarGastos(ListaTextViewList.java:92) 07-09 21:45:26.715: E/AndroidRuntime(13481): at controlador.AdminUI.establecerListaGastoVar(AdminUI.java:138) 07-09 21:45:26.715: E/AndroidRuntime(13481): at com.ConApps.walletsaver.GastosVariables.onCreate(GastosVariables.java:23) 07-09 21:45:26.715: E/AndroidRuntime(13481): at android.app.Activity.performCreate(Activity.java:4465) 07-09 21:45:26.715: E/AndroidRuntime(13481): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 07-09 21:45:26.715: E/AndroidRuntime(13481): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932) 07-09 21:45:26.715: E/AndroidRuntime(13481): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993) 07-09 21:45:26.715: E/AndroidRuntime(13481): at android.app.ActivityThread.access$600(ActivityThread.java:127) 07-09 21:45:26.715: E/AndroidRuntime(13481): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159) 07-09 21:45:26.715: E/AndroidRuntime(13481): at android.os.Handler.dispatchMessage(Handler.java:99) 07-09 21:45:26.715: E/AndroidRuntime(13481): at android.os.Looper.loop(Looper.java:137) 07-09 21:45:26.715: E/AndroidRuntime(13481): at android.app.ActivityThread.main(ActivityThread.java:4507) 07-09 21:45:26.715: E/AndroidRuntime(13481): at java.lang.reflect.Method.invokeNative(Native Method) 07-09 21:45:26.715: E/AndroidRuntime(13481): at java.lang.reflect.Method.invoke(Method.java:511) 07-09 21:45:26.715: E/AndroidRuntime(13481): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 07-09 21:45:26.715: E/AndroidRuntime(13481): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 07-09 21:45:26.715: E/AndroidRuntime(13481): at dalvik.system.NativeStart.main(Native Method) 
+4
source share
2 answers

setBackground() introduced only in 16 api . Use setBackgroundDrawable() .

that all phones that have less than 16 api will support setBackgroundDrawable()

+10
source

Workaround for this problem:

 Drawable d = this.res.getDrawable(R.drawable.metro_circle); if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN ) { metroIconTextView.setBackground(d); } else { metroIconTextView.setBackgroundDrawable(d); } 
+6
source

Source: https://habr.com/ru/post/1490620/


All Articles