在Android中发布新活动

时间:2013-08-16 12:39:50

标签: android

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 个答案:

答案 0 :(得分:1)

你打电话给你 完() 方法..

btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

            finish();
        }
    });

在你的第二个活动中

答案 1 :(得分:0)

尝试这样的事情:

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

仅在您希望活动在转移到下一个活动后退出时才调用结束。

答案 2 :(得分:0)

尝试这样: 打电话给onclick methed:

finish();
System.gc();

完成活动并返回上一个活动

答案 3 :(得分:-1)

您的CopperChimneyDesc.class一定有问题。 查看onCreate函数。特别是在第39行。有一个nullpointer

返回功能:

@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();
   }
}