使用TextView方法获取NoSuchMethodError

时间:2013-07-10 03:56:38

标签: android view textview nosuchmethoderror

我正在获得一个NoSuchMethodError,其中包含一个扩展TextView的类。我唯一做的就是添加一些变量并添加onLongClickListener。没有更多修改。

当我在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?我在创建项目时将minimun设置为2.2。我正在使用android.support.v4库来请求它们。

这是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)

2 个答案:

答案 0 :(得分:10)

setBackground()仅在16 api中引入。请改用setBackgroundDrawable()

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

答案 1 :(得分:6)

这是此问题的解决方法:

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