1

我有 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 中的条件为真,新活动即将出现,但几秒钟后应用程序停止。我已经使用了注册页面,但我以不同的方式调用它,一切都很好,所以我想我调用它的方式有问题。在注册活动运行时,我调用 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);
        }
    }

编辑 日志猫:

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

4

4 回答 4

2

//this for the second question

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.
     }

// And

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;
    }
于 2012-12-31T09:32:32.943 回答
0

当您在 Activity 中运行时,您正试图停止服务。删除线

this.stopService(this.getIntent());

无论如何,您调用其他活动的方式都没有问题。只是你试图停止一项服务,作为一个选项提供一个引用 Activity 的 Intent - 这可能是你应该遇到的 Logcat 错误。

如果您没有具体理由真正关闭 Activity,请保持 Activity 原样并让堆栈按预期工作!

将此添加到您的活动中:

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

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

于 2012-12-31T09:27:28.833 回答
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;
     }
于 2012-12-31T09:36:47.623 回答
0

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

感谢您的评论和帮助。

于 2012-12-31T09:38:58.327 回答