Android 使用TextView方法获取NoSuchMethodError

Android 使用TextView方法获取NoSuchMethodError,android,view,textview,nosuchmethoderror,Android,View,Textview,Nosuchmethoderror,我得到了一个NoSuchMethodError,其中有一个类扩展了TextView。我做的唯一一件事就是给它添加一些变量,然后添加onlongclicklister。没有更多的修改 当我在android手机4.1.2中使用我的应用程序时,一切都很好 但是在我朋友的手机4.0.3中,它抛出了这个NoSuchMethodError 以下是我创建扩展Textview的类时的代码: descrip=new TextViewList(context, admin, this); descrip.setPa

我得到了一个
NoSuchMethodError
,其中有一个类扩展了
TextView
。我做的唯一一件事就是给它添加一些变量,然后添加
onlongclicklister
。没有更多的修改

当我在android手机4.1.2中使用我的应用程序时,一切都很好 但是在我朋友的手机4.0.3中,它抛出了这个
NoSuchMethodError

以下是我创建扩展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);
首先,它使用
setTextAlignment
抛出异常,然后我将其删除,并使用
setBackground
方法再次抛出异常

是什么导致了这个错误?这是否意味着我的应用程序与Android不兼容 低于4.1.2的版本?创建项目时,我将最小值设置为2.2。我正在使用android.support.v4库

这是日志:

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)
setBackground()
仅在
16 api
中引入。改用
setBackgroundDrawable()


对于所有api少于16的手机,它们将支持
setBackgroundDrawable()

这是解决此问题的一种方法:

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);
}

. 检查支持的方法所以我只需要查找旧版本android支持的方法?在本例中,对于这个项目,我将minimun api设置为8。如果没有针对这些旧API的方法,是否还有其他替代方法?谢谢你的回答:)同样的事情我写了如果你不能使用setbackground,就使用setbackgrounddrawable。这是只有低版本的替代方案…如果你想要更多的澄清做一些研发。如果你喜欢这个,就把它做好。是的,这就是工作。然后我会确保使用正确的方法来支持较低版本。谢谢你的帮助眼镜王蛇先生