我正在尝试为android微调器实现自定义微调器样式。当我在微调器中选择一个它没有在微调器上显示的项目(所选项目)时出现问题。我的代码中有几个问题。 这是日志猫
01-06 08:21:38.254: E/AndroidRuntime(733): FATAL EXCEPTION: main
01-06 08:21:38.254: E/AndroidRuntime(733): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-06 08:21:38.254: E/AndroidRuntime(733): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-06 08:21:38.254: E/AndroidRuntime(733): at java.util.ArrayList.get(ArrayList.java:304)
01-06 08:21:38.254: E/AndroidRuntime(733): at com.xx.x.Page5SubActivity$MyAdapter.getCustomView(Page5SubActivity.java:487)
01-06 08:21:38.254: E/AndroidRuntime(733): at com.xx.x.Page5SubActivity$MyAdapter.getView(Page5SubActivity.java:479)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.Spinner.onMeasure(Spinner.java:285)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-06 08:21:38.254: E/AndroidRuntime(733): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.os.Looper.loop(Looper.java:137)
01-06 08:21:38.254: E/AndroidRuntime(733): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-06 08:21:38.254: E/AndroidRuntime(733): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 08:21:38.254: E/AndroidRuntime(733): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 08:21:38.254: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-06 08:21:38.254: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-06 08:21:38.254: E/AndroidRuntime(733): at dalvik.system.NativeStart.main(Native Method)
这是我用来定制微调器的类
public class MyAdapter extends ArrayAdapter<String>
{
private List<String> listString = new ArrayList<String>();
public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
//listString = objects;
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.spinner, parent, false);
TextView label=(TextView)row.findViewById(R.id.textView1);
label.setText(listString.get(position));
return row;
}
}
当我对此行//listString = objects;
发表评论时,错误已开始
我对它进行了评论,因为它已经用super方法初始化了。 super(context, textViewResourceId, objects);
任何人都能解释一下这个吗?在OOP中,我们使用super来初始化具有超类值的内容。那么为什么我需要重新初始化呢?或者我们不能在2个参数中使用super方法并在上面的类中初始化//listString = objects;
?我知道我缺少OOP。任何人都可以解释一下吗?
第二个问题是当我删除评论时它开始起作用。但它没有显示我选择的项目。我已经写过项目选定的事件。他们的作品。我还需要做些什么呢?
答案 0 :(得分:1)
listString
无法由超类初始化。它是MyAdapter
类的成员,而不是超类。
private List<String> listString = new ArrayList<String>();
get
来电listString
时仍然为空,因此也是例外。