在活动之间移动

时间:2012-12-31 08:58:47

标签: java android android-activity

我有2个活动,1个是我的应用程序的主页面,另一个是注册活动。 我的主页的主体是这样的:

 public void onCreate(Bundle savedInstanceState)
 {
     registerOperation();

     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     System.out.println("Already logged in");
 }
 public void registerOperation()
 {
     //checks if I did logged in already. if true start a new Intent of my Registeration page and stop the current Intent.
 }

在我的测试中我还没有注册,所以registerOperation中的条件是真的,新的活动即将到来,但几秒钟之后应用就停止了。 我已经使用了registeration页面,但是我用一种不同的方式调用它,一切都很好,所以我猜我称它的方式有问题。 在调用registerOperation之后,输出行也会在寄存器活动运行时完成。

修改 如果您想查看特定代码,请告知我,我将更新帖子。

传递活动代码。

    public void registerOperation()
    {
        SharedPreferences pref = getSharedPreferences("MyKid",MODE_PRIVATE);
        String email = pref.getString("email", null);
        String password = pref.getString("password", null);
        if(email == null && password == null)
        {
            this.stopService(this.getIntent());
            Intent register = new Intent(getBaseContext(), Register.class);
            startActivity(register);
        }
    }

修改 logcat的:

  

12-31 11:27:38.610:E / AndroidRuntime(28903):java.lang.RuntimeException:无法停止活动{com.example.android.location / com.example.android.location.Track}:java .lang.NullPointerException

4 个答案:

答案 0 :(得分:2)

//这是第二个问题

public void onCreate(Bundle savedInstanceState)
     {
         if (registerOperation()){
             System.out.println("Already logged in");
             return;
         } 

         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);


     }
     public void registerOperation()
     {
         //checks if I did logged in already. if true start a new Intent of my Registeration page and stop the current Intent.
     }

//和

public boolean registerOperation()
    {
        SharedPreferences pref = getSharedPreferences("MyKid",MODE_PRIVATE);
        String email = pref.getString("email", null);
        String password = pref.getString("password", null);
        if(email == null && password == null)
        {
            this.stopService(this.getIntent());
            Intent register = new Intent(getBaseContext(), Register.class);
            startActivity(register);
            return true;
        }
        return false;
    }

答案 1 :(得分:0)

您正在尝试在活动中运行时停止服务。删除行

this.stopService(this.getIntent());

无论如何,你打电话给其他活动的方式都没有错。只是你试图停止一个服务,作为一个选项提供一个指向一个活动的Intent - 这可能是你应该有的Logcat错误。

如果您没有特别的理由真正关闭活动,只需将活动保持原样并让堆栈按预期工作!

将此添加到您的活动:

@Override
protected void onStop() {
    super.onStop();
    finish();
}

这样,当您切换到另一个时,您的活动将被正确销毁。

答案 2 :(得分:0)

尝试类似这样的内容::

请尝试从onCreate()::

中删除不必要的代码
 public void onCreate(Bundle savedInstanceState)
 {
  boolean a = registerOperation();

 super.onCreate(savedInstanceState);

  if(a)
    { 
       setContentView(R.layout.main);

       System.out.println("Already logged in"); }
     }
 public boolean registerOperation()

     {


    SharedPreferences pref = getSharedPreferences("MyKid",MODE_PRIVATE);
    String email = pref.getString("email", null);
    String password = pref.getString("password", null);
    if(email == null && password == null)
    {   return false; //Using these flags you can find out whether to redirect to Registration or Already Logged in Page
        this.stopService(this.getIntent());
        Intent register = new Intent(getBaseContext(), Register.class);
        startActivity(register);
    }
      return true;
     }

答案 3 :(得分:0)

我的应用程序停止的原因是因为我重写了onStop方法,在其中我使用了一些未初始化的对象。 我改变了构建应用程序的方式,我猜这就是我忘记这些对象的原因。

感谢您的评论和帮助。

相关问题