这里我创建了四个xml文件main.xml
, test1.xml
, test2.xml
, test3.xml
.
可以访问它们,spinner
但我收到了异常。我也给logcat。
请帮我。
String[] lan = { "test1", "test2", "test3" };
Spinner spn = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, lan);
spn.setAdapter(adapter);
spn.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
if (pos == 0) {
findViewById(R.layout.test1).setVisibility(View.VISIBLE);
} else if (pos == 1) {
findViewById(R.layout.test2).setVisibility(View.VISIBLE);
} else if (pos == 2) {
findViewById(R.layout.test3).setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
日志猫:
07-11 18:09:39.480: E/AndroidRuntime(21200): FATAL EXCEPTION: main
07-11 18:09:39.480: E/AndroidRuntime(21200): java.lang.NullPointerException
07-11 18:09:39.480: E/AndroidRuntime(21200): at com.venky.innverlayoutchangevisibility.MainActivity$1.onItemSelected(MainActivity.java:30)
07-11 18:09:39.480: E/AndroidRuntime(21200): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
07-11 18:09:39.480: E/AndroidRuntime(21200): at android.widget.AdapterView.access$200(AdapterView.java:42)
07-11 18:09:39.480: E/AndroidRuntime(21200): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
07-11 18:09:39.480: E/AndroidRuntime(21200): at android.os.Handler.handleCallback(Handler.java:587)
07-11 18:09:39.480: E/AndroidRuntime(21200): at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 18:09:39.480: E/AndroidRuntime(21200): at android.os.Looper.loop(Looper.java:130)
07-11 18:09:39.480: E/AndroidRuntime(21200): at android.app.ActivityThread.main(ActivityThread.java:3691)
07-11 18:09:39.480: E/AndroidRuntime(21200): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 18:09:39.480: E/AndroidRuntime(21200): at java.lang.reflect.Method.invoke(Method.java:507)
07-11 18:09:39.480: E/AndroidRuntime(21200): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
07-11 18:09:39.480: E/AndroidRuntime(21200): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
07-11 18:09:39.480: E/AndroidRuntime(21200): at dalvik.system.NativeStart.main(Native Method)