我有一个带有 ExpandableListView 的应用程序,每个标题都有它自己的可绘制作为背景。这是代码:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB);
{
switch(groupPosition){
case 0:
convertView.setBackground(this._context.getResources().getDrawable(R.drawable.light_blue));
break;
case 1:
convertView.setBackground(this._context.getResources().getDrawable(R.drawable.drugi_letniki_color));
break;
case 2:
convertView.setBackground(this._context.getResources().getDrawable(R.drawable.light_blue));
break;
case 3:
convertView.setBackground(this._context.getResources().getDrawable(R.drawable.cetrti_letniki_color));
break;
}
}
这在我的带有 Android 4.3 的三星 Galaxy Nexus 上完全可以正常工作,但是当我在带有 Android 2.2 版本的模拟器上尝试它时它崩溃了。
这是 logcat 文件:
09-05 20:46:45.914: E/AndroidRuntime(371): FATAL EXCEPTION: main
09-05 20:46:45.914: E/AndroidRuntime(371): java.lang.NoSuchMethodError: android.view.View.setBackground
09-05 20:46:45.914: E/AndroidRuntime(371): at com.whizzapps.stpsurniki.ExpandableListAdapter.getGroupView(ExpandableListAdapter.java:100)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.ListView.makeAndAddView(ListView.java:1727)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.ListView.fillDown(ListView.java:652)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.ListView.fillFromTop(ListView.java:709)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.ListView.layoutChildren(ListView.java:1580)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.view.View.layout(View.java:7035)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1238)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.LinearLayout.onLayout(LinearLayout.java:1044)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.view.View.layout(View.java:7035)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.view.View.layout(View.java:7035)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.view.View.layout(View.java:7035)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.view.View.layout(View.java:7035)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.os.Looper.loop(Looper.java:123)
09-05 20:46:45.914: E/AndroidRuntime(371): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-05 20:46:45.914: E/AndroidRuntime(371): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 20:46:45.914: E/AndroidRuntime(371): at java.lang.reflect.Method.invoke(Method.java:521)
09-05 20:46:45.914: E/AndroidRuntime(371): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-05 20:46:45.914: E/AndroidRuntime(371): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-05 20:46:45.914: E/AndroidRuntime(371): at dalvik.system.NativeStart.main(Native Method)
这是第 100 行:
convertView.setBackground(this._context.getResources().getDrawable(R.drawable.light_blue));
如您所见,我已经尝试通过禁用旧版本的自定义背景来解决问题,但它似乎忽略了 if 语句。
编辑:如果有人仍然想知道我到底是如何解决问题的;这是代码:
if (Build.VERSION.SDK_INT >= 16)
{
switch(groupPosition){
case 0:
convertView.setBackground(this._context.getResources().getDrawable(R.drawable.light_blue));
break;
case 1:
convertView.setBackground(this._context.getResources().getDrawable(R.drawable.drugi_letniki_color));
break;
case 2:
convertView.setBackground(this._context.getResources().getDrawable(R.drawable.light_blue));
break;
case 3:
convertView.setBackground(this._context.getResources().getDrawable(R.drawable.cetrti_letniki_color));
break;
}
}
else if (Build.VERSION.SDK_INT < 16)
{
switch(groupPosition){
case 0:
convertView.setBackgroundDrawable(this._context.getResources().getDrawable(R.drawable.light_blue));
break;
case 1:
convertView.setBackgroundDrawable(this._context.getResources().getDrawable(R.drawable.drugi_letniki_color));
break;
case 2:
convertView.setBackgroundDrawable(this._context.getResources().getDrawable(R.drawable.light_blue));
break;
case 3:
convertView.setBackgroundDrawable(this._context.getResources().getDrawable(R.drawable.cetrti_letniki_color));
break;
}
}
这是因为 .setBackgroundDrawable 在 API 1 及更高版本上可用,而 .setBackground 仅在 API 16 及更高版本上可用