我有一个包含 TextView、ListView 和 Spinner 的活动。微调器包含 4 个项目。onCreate 时,为微调器分配与当前活动匹配的值之一。当用户单击另一个项目时,必须启动一个新的 Intent(具有相同布局的新活动)。但是,当我未注释有关微调器的代码时,我会收到错误消息。如果没有“实现 OnItemSelectedListener”和事件的实现,我的 Activity 可以正常工作。
所以我的微调器代码:
public class ViziteIeri extends Activity implements OnItemSelectedListener{
...
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_new_screen);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.liste_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
Toast toast1 = Toast.makeText(getApplicationContext(),""+parent.getItemAtPosition(pos), Toast.LENGTH_SHORT);
toast1.show();
Intent i = null;
if(parent.getItemIdAtPosition(pos) == 1) {
i=new Intent(ViziteIeri.this, ViziteAzi.class);
}
if(parent.getItemIdAtPosition(pos) == 2) {
i=new Intent(ViziteIeri.this, ViziteMaine.class);
}
if(parent.getItemIdAtPosition(pos) == 3) {
i=new Intent(ViziteIeri.this, ViziteViitor.class);
}
startActivity(i);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
那么我做错了什么?抱歉之前的代码。我只是出于粘贴原因更改了活动的名称。现在它们是固定的。在我的真实应用程序中,当然活动是正确命名的,否则我无法运行应用程序(对吗?)
@CodeMonkey - 这是错误日志:
`07-29 23:56:56.155: D/AndroidRuntime(2474): Shutting down VM
07-29 23:56:56.155: W/dalvikvm(2474): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-29 23:56:56.174: E/AndroidRuntime(2474): FATAL EXCEPTION: main
07-29 23:56:56.174: E/AndroidRuntime(2474): java.lang.NullPointerException
07-29 23:56:56.174: E/AndroidRuntime(2474): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
07-29 23:56:56.174: E/AndroidRuntime(2474): at android.app.Activity.startActivityForResult(Activity.java:2827)
07-29 23:56:56.174: E/AndroidRuntime(2474): at android.app.Activity.startActivity(Activity.java:2933)
07-29 23:56:56.174: E/AndroidRuntime(2474): at com.softwarex.mydualscreenpro.ViziteIeri.onItemSelected(ViziteIeri.java:89)
07-29 23:56:56.174: E/AndroidRuntime(2474): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
07-29 23:56:56.174: E/AndroidRuntime(2474): at android.widget.AdapterView.access$200(AdapterView.java:42)
07-29 23:56:56.174: E/AndroidRuntime(2474): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
07-29 23:56:56.174: E/AndroidRuntime(2474): at android.os.Handler.handleCallback(Handler.java:587)
07-29 23:56:56.174: E/AndroidRuntime(2474): at android.os.Handler.dispatchMessage(Handler.java:92)
07-29 23:56:56.174: E/AndroidRuntime(2474): at android.os.Looper.loop(Looper.java:123)
07-29 23:56:56.174: E/AndroidRuntime(2474): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-29 23:56:56.174: E/AndroidRuntime(2474): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 23:56:56.174: E/AndroidRuntime(2474): at java.lang.reflect.Method.invoke(Method.java:507)
07-29 23:56:56.174: E/AndroidRuntime(2474): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-29 23:56:56.174: E/AndroidRuntime(2474): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-29 23:56:56.174: E/AndroidRuntime(2474): at dalvik.system.NativeStart.main(Native Method)`
我改回了活动名称,因为它变得令人困惑。所以活动名称是“ViziteIeri”再次......如果我让事件分配取消注释,我无法在没有收到上述错误堆栈的情况下运行活动