如何从一个活动导航到另一个活动

时间:2014-02-27 12:05:31

标签: java android eclipse

我正在尝试在Android应用程序的不同页面之间导航,当我运行项目时,它会将我带到模拟器屏幕,但会显示一条错误消息:

enter image description here

以下是.java中的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Spinner spinner = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
    this, R.array.MyPictures, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);


    final Button button = (Button) findViewById(R.id.button02);
    button.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            setContentView(R.layout.imageview);
        }
    });

}

LogCat:

02-27 11:58:41.374: D/AndroidRuntime(313): Shutting down VM
02-27 11:58:41.374: W/dalvikvm(313): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-27 11:58:41.454: E/AndroidRuntime(313): FATAL EXCEPTION: main
02-27 11:58:41.454: E/AndroidRuntime(313): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
02-27 11:58:41.454: E/AndroidRuntime(313):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-27 11:58:41.454: E/AndroidRuntime(313):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-27 11:58:41.454: E/AndroidRuntime(313):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-27 11:58:41.454: E/AndroidRuntime(313):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-27 11:58:41.454: E/AndroidRuntime(313):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 11:58:41.454: E/AndroidRuntime(313):  at android.os.Looper.loop(Looper.java:123)
02-27 11:58:41.454: E/AndroidRuntime(313):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-27 11:58:41.454: E/AndroidRuntime(313):  at java.lang.reflect.Method.invokeNative(Native Method)
02-27 11:58:41.454: E/AndroidRuntime(313):  at java.lang.reflect.Method.invoke(Method.java:521)
02-27 11:58:41.454: E/AndroidRuntime(313):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-27 11:58:41.454: E/AndroidRuntime(313):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-27 11:58:41.454: E/AndroidRuntime(313):  at dalvik.system.NativeStart.main(Native Method)
02-27 11:58:41.454: E/AndroidRuntime(313): Caused by: java.lang.NullPointerException
02-27 11:58:41.454: E/AndroidRuntime(313):  at com.example.test.MainActivity.onCreate(MainActivity.java:29)
02-27 11:58:41.454: E/AndroidRuntime(313):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-27 11:58:41.454: E/AndroidRuntime(313):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-27 11:58:41.454: E/AndroidRuntime(313):  ... 11 more

有什么错误我不知道???

这是我的第二个显示相同错误的项目 感谢

1 个答案:

答案 0 :(得分:4)

你有

final Button button = (Button) findViewById(R.id.button02);
    button.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            setContentView(R.layout.imageview);
        }
    });

从按钮点击中删除此setContentView(R.layout.imageview);。同时删除final并在全球范围内声明spinnerbutton

所以你的按钮点击事件应该是这样的。

final Button button = (Button) findViewById(R.id.button02);
        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                // Do something here
            }
        });

基本上你的完整活动就是这样的。

Button button;
Spinner spinner; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    spinner = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
    this, R.array.MyPictures, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);


    button = (Button) findViewById(R.id.button02);
    button.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
          Intent mSignup = new Intent(activityOne.this, activityTwo.class);
          startActivity(mSignup);
        }
    });

}

如果您正在使用imageview,请在onCreate方法中设置它,然后尝试。