0
I am trying to learn screen navigation in android

我试图实现如下

  • 当我单击活动中的按钮时,它会启动一个新活动
  • 但是该最终活动必须在单击时包含一个后退按钮,它应该返回第一个活动

生病在下面发布一些描述性代码

CopperchimneyDesc.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.copperchimney_desc_screen);


Button PHOTOBUTTON=(Button) findViewById(R.id.CopperChimneyPhotosButton);
            PHOTOBUTTON.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent pht=new Intent(CopperChimneyDesc.this,CopperChimneyPhotos.class);
                    startActivity(pht); 
                }
            });

CopperChimneyPhotos.java

public class CopperChimneyPhotos extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.copper_chimney_photos);


        Button btn=(Button) findViewById(R.id.PhotoButton);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent pt=new Intent(CopperChimneyPhotos.this,CopperChimneyDesc.class);
                startActivity(pt);
            }
        });
    }

PS:: 我已经在清单中声明了新活动

我的错误日志如下::

08-16 17:42:33.730: E/AndroidRuntime(444): FATAL EXCEPTION: main
08-16 17:42:33.730: E/AndroidRuntime(444): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.findmybuffet/com.project.findmybuffet.CopperChimneyDesc}: java.lang.NullPointerException
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.os.Looper.loop(Looper.java:123)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 17:42:33.730: E/AndroidRuntime(444):  at java.lang.reflect.Method.invokeNative(Native Method)
08-16 17:42:33.730: E/AndroidRuntime(444):  at java.lang.reflect.Method.invoke(Method.java:507)
08-16 17:42:33.730: E/AndroidRuntime(444):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 17:42:33.730: E/AndroidRuntime(444):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 17:42:33.730: E/AndroidRuntime(444):  at dalvik.system.NativeStart.main(Native Method)
08-16 17:42:33.730: E/AndroidRuntime(444): Caused by: java.lang.NullPointerException
08-16 17:42:33.730: E/AndroidRuntime(444):  at com.project.findmybuffet.CopperChimneyDesc.onCreate(CopperChimneyDesc.java:39)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 17:42:33.730: E/AndroidRuntime(444):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-16 17:42:33.730: E/AndroidRuntime(444):  ... 11 more

关于如何克服这个问题的任何想法,我都粘贴了相关代码。

4

4 回答 4

1

你只需调用finish()方法..

btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            finish();
        }
    });

在你的第二个活动中

于 2013-08-16T12:44:15.687 回答
0

试试这样:调用 onclick 方法:

finish();
System.gc();

完成活动并返回之前的活动

于 2013-08-16T13:09:13.760 回答
0

尝试这样的事情:

Intent goToMainScreen = new Intent("com.dereaper.goalsmanager.MAINSCREEN");
startActivity(goToMainScreen);
finish(); 

仅当您希望活动在移动到下一个活动后退出时才调用完成。

于 2013-08-16T12:42:55.417 回答
-1

您的 CopperChimneyDesc.class 一定有问题。查看 onCreate 函数。尤其是在第 39 行。有一个空指针

返回函数:

@Override
public void onClick(View v) {
   if(v == PHOTOBUTTON){
       // TODO Auto-generated method stub
       Intent pht=new Intent(CopperChimneyDesc.this,CopperChimneyPhotos.class);
       startActivity(pht); 
   }
   if(v == OTHERBUTTON){
       finish();
   }
}
于 2013-08-16T12:47:21.217 回答