我正在嘗試爲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來初始化具有超類值的內容。那麼爲什麼我需要重新初始化呢?或者我們不能在兩個參數中使用超級方法,並且在上面的類中初始化//listString = objects;
?我知道我缺乏面向對象。有人可以解釋我嗎?
第二個問題是當我刪除註釋開始工作。但它沒有顯示我選擇的項目。我已經寫了項目選定的事件。他們工作。還有什麼我需要做的工作呢?
我可以保證你的類中的一個私有變量* *沒有被調用'super()'初始化。父類如何知道在子類中聲明的私有變量的任何內容? –
是的我已經聲明'私人列表 listString = new ArrayList ();'私人的 。 PLZ給我一些時間,直到項目建成 –
newday
我還有第二個問題。爲什麼Spinner沒有在旋轉器上顯示選定的項目? – newday