我使用以下代码spinner1
作为产品选择,我必须spinner2
根据spinner1
内容提供数据。spinner2
应该显示项目,但是当我运行它时它会被强制关闭。有人可以分享你的经验吗?
public class SpinnerActivity extends Activity {
private String[] spinner1values = new String[] { "Automobiles", "Jobs" };
private String[][] spinner2values = new String[][] {
new String[] { "a1", "b1", "c1" },
new String[] { "a2", "b2" }
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinner1values);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// create a new adapter with the corresponding values
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
android.R.layout.simple_spinner_item, spinner2values[position]);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set adapter
((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
}
public void onNothingSelected(AdapterView<?> arg0) {
// nothing selected, so set empty options
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
android.R.layout.simple_spinner_item, new String[0]);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
}
});
}
}
日志错误
11-08 17:28:13.013: E/AndroidRuntime(1719): FATAL EXCEPTION: main
11-08 17:28:13.013: E/AndroidRuntime(1719): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.spinnersample/com.spinnersample.Spinner}: java.lang.ClassNotFoundException: com.spinnersample.Spinner
11-08 17:28:13.013: E/AndroidRuntime(1719): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
11-08 17:28:13.013: E/AndroidRuntime(1719): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-08 17:28:13.013: E/AndroidRuntime(1719): at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-08 17:28:13.013: E/AndroidRuntime(1719): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-08 17:28:13.013: E/AndroidRuntime(1719): at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 17:28:13.013: E/AndroidRuntime(1719): at android.os.Looper.loop(Looper.java:137)
11-08 17:28:13.013: E/AndroidRuntime(1719): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-08 17:28:13.013: E/AndroidRuntime(1719): at java.lang.reflect.Method.invokeNative(Native Method)
11-08 17:28:13.013: E/AndroidRuntime(1719): at java.lang.reflect.Method.invoke(Method.java:511)
11-08 17:28:13.013: E/AndroidRuntime(1719): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-08 17:28:13.013: E/AndroidRuntime(1719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-08 17:28:13.013: E/AndroidRuntime(1719): at dalvik.system.NativeStart.main(Native Method)
11-08 17:28:13.013: E/AndroidRuntime(1719): Caused by: java.lang.ClassNotFoundException: com.spinnersample.Spinner
11-08 17:28:13.013: E/AndroidRuntime(1719): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-08 17:28:13.013: E/AndroidRuntime(1719): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-08 17:28:13.013: E/AndroidRuntime(1719): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-08 17:28:13.013: E/AndroidRuntime(1719): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-08 17:28:13.013: E/AndroidRuntime(1719): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
11-08 17:28:13.013: E/AndroidRuntime(1719): ... 11 more