我知道这个问题已被多次讨论过了。我尽我所能,安装了源代码。关键点:我正在使用Google sdk - 10,我正在尝试使用此示例实现微调器:http://developer.android.com/resources/tutorials/views/hello-spinner.html并且仍然存在问题。 这是调试消息:
AlertController$RecycleListView(ViewGroup).getChildAt(int) line: 3065
/**
* Returns the view at the specified position in the group.
*
* @param index the position at which to get the view from
* @return the view at the specified position or null if the position
* does not exist within the group
*/
public View getChildAt(int index) {
try {
return mChildren[index];
} catch (IndexOutOfBoundsException ex) {
return null;
}
}
这里停止执行:
return mChildren[index];
我做错了什么?
这是主要课程:
package com.hellospinner.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class HelloSpinnerActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
}
这是我的OnItemSelectedListener类:
package com.hellospinner.test;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{ Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); }
public void onNothingSelected(AdapterView parent)
{ }
}
这是main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/planet_prompt"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/planet_prompt"
/>
</LinearLayout>
来自strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloSpinner</string>
<string name="planet_prompt">Choose a planet</string>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
这里是logcat
Thread [<1> main] (Suspended (exception ArrayIndexOutOfBoundsException))
AlertController$RecycleListView(ViewGroup).getChildAt(int) line: 3065
AlertController$RecycleListView(ListView).layoutChildren() line: 1494
AlertController$RecycleListView(AbsListView).onLayout(boolean, int, int, int, int) line: 1260
AlertController$RecycleListView(View).layout(int, int, int, int) line: 7175
LinearLayout.setChildFrame(View, int, int, int, int) line: 1254
LinearLayout.layoutVertical() line: 1130
LinearLayout.onLayout(boolean, int, int, int, int) line: 1047
LinearLayout(View).layout(int, int, int, int) line: 7175
WeightedLinearLayout(LinearLayout).setChildFrame(View, int, int, int, int) line: 1254
WeightedLinearLayout(LinearLayout).layoutVertical() line: 1130
WeightedLinearLayout(LinearLayout).onLayout(boolean, int, int, int, int) line: 1047
WeightedLinearLayout(View).layout(int, int, int, int) line: 7175
FrameLayout.onLayout(boolean, int, int, int, int) line: 338
FrameLayout(View).layout(int, int, int, int) line: 7175
PhoneWindow$DecorView(FrameLayout).onLayout(boolean, int, int, int, int) line: 338
PhoneWindow$DecorView(View).layout(int, int, int, int) line: 7175
ViewRoot.performTraversals() line: 1140
ViewRoot.handleMessage(Message) line: 1859
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 130
ActivityThread.main(String[]) line: 3683
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]