我有一个带有ExpandableListView的应用程序,每个标题都有自己的drawable作为背景。这是代码:
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;
}
}
这在我的三星Galaxy Nexus上使用Android 4.3完全正常,但是当我在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及更高版本上可用
答案 0 :(得分:0)
检查可能无效,因为2.2中未定义Build.VERSION_CODES.HONEYCOMB
。
如果你想进行检查,你只需要使用常数。
修改强>
此外,在if
声明后面有一个分号。你应该删除它,这样你的条件实际上会控制你是否进入if
语句的代码块。