我正在尝试为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来初始化具有超类值的内容。那么,为什么我需要重新初始化呢?还是我们不能将super方法用于2个参数并在上述类中初始化//listString = objects;?我知道我没有OOP。有人可以向我解释吗?

第二个问题是,当我删除评论时,它开始起作用。但是它没有显示我选择的项目。我已经写了项目选定的事件。他们工作。我还要做些什么来工作呢?

最佳答案

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

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


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

关于java - 索引超出范围异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14178881/

10-08 21:21