gpt4 book ai didi

java - 在 Activity 之间移动

转载 作者:行者123 更新时间:2023-11-29 21:52:16 25 4
gpt4 key购买 nike

我有 2 个 Activity ,一个是我的应用程序主页,另一个是注册页面。我主页的正文是这样的:

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

编辑如果您想查看特定代码,请告诉我,我会更新帖子。

传递 Activity 代码。

    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: Unable to stop activity {com.example.android.location/com.example.android.location.Track}: java.lang.NullPointerException

最佳答案

//这是第二个问题

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

关于java - 在 Activity 之间移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14099568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com