1

我正在尝试为 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(context, textViewResourceId, objects);有人可以解释一下吗?在 OOP 中,我们使用 super 来初始化具有超类值的内容。那么为什么我需要重新初始化呢?或者我们不能对2个参数使用超级方法并//listString = objects;在上面的类中初始化吗?我知道我缺乏 OOP。有人可以解释一下吗?

第二个问题是当我删除评论时它开始起作用。但它没有显示我选择的项目。我已经写过项目选择事件。他们工作。我还需要做什么才能完成这项工作?

4

1 回答 1

1

listString不能被超类初始化。它是MyAdapter类的成员,而不是超类。

private List<String> listString = new ArrayList<String>();

get调用的时候listString仍然是空的,因此异常。

于 2013-01-06T03:12:02.280 回答