gpt4 book ai didi

android - 登录屏幕内存功能

转载 作者:行者123 更新时间:2023-11-29 01:53:09 26 4
gpt4 key购买 nike

我是 android 的新手,这个问题是重新发布的,因为某些原因我无法更新以前的问题。我可以在我的登录页面中创建记住我的功能,当我重新启动我的应用程序时,用户名和密码字段已经填写,但我必须单击登录按钮才能进入另一个 Activity 。

如果我想在记住我复选框为真时直接加载另一个 Activity 而不是在我再次加载我的应用程序时再次单击登录按钮,如何编码。

  public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b = (Button)findViewById(R.id.Button01);
et = (EditText)findViewById(R.id.username);
pass= (EditText)findViewById(R.id.password);
register = (TextView)findViewById(R.id.register);
saveLoginCheckBox = (CheckBox)findViewById(R.id.remember);
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();

saveLogin = loginPreferences.getBoolean("saveLogin", false);
if (saveLogin == true) {
//uname = et.getText().toString();
// pwd = pass.getText().toString();
et.setText(loginPreferences.getString("uname", ""));
pass.setText(loginPreferences.getString("pwd", ""));
saveLoginCheckBox.setChecked(true);
login();
// finish();
}



b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
uname = et.getText().toString();
pwd = pass.getText().toString();

if (saveLoginCheckBox.isChecked()) {
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.putString("uname", uname);
loginPrefsEditor.putString("pwd", pwd);
loginPrefsEditor.commit();
} else {
loginPrefsEditor.clear();
loginPrefsEditor.commit();
}


login();

}
}).start();
}
});



register.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//dialog = ProgressDialog.show(MainActivity.this, "",
// "Loading...", true);
new Thread(new Runnable(){
public void run(){

register();
}
}).start();
}
});

}



void register(){
startActivity(new Intent(MainActivity.this,RegisterPage.class));


}

void login(){
try{

httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://192.168.10.71/login.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
// response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
// System.out.println("Response : " + response);
/* runOnUiThread(new Runnable() {
public void run() {
tv.setText(response + et.getText());
dialog.dismiss();
}
});
*/
if(response.equalsIgnoreCase("Doctor Found")){
runOnUiThread(new Runnable() {
public void run() {
//tv.setText(response);
dialog.dismiss();
Toast.makeText(MainActivity.this,"Hello Doctor " + et.getText(), Toast.LENGTH_SHORT).show();
}
});

startActivity(new Intent(MainActivity.this, DoctorPage.class));

}
else if(response.equalsIgnoreCase("Nurse Found")){
runOnUiThread(new Runnable() {
public void run() {
//tv.setText(response);
dialog.dismiss();
Toast.makeText(MainActivity.this,"Hello Nurse " + et.getText(), Toast.LENGTH_SHORT).show();
}
});

startActivity(new Intent(MainActivity.this, NursePage.class));

}
else{
showAlert();
dialog.dismiss();
}

}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}

我收到 Null Pointer Exception 。如果选中记住我框,我将无法切换到其他 Activity 。这是堆栈跟踪。

05-21 04:13:02.284: E/AndroidRuntime(3397): FATAL EXCEPTION: main
05-21 04:13:02.284: E/AndroidRuntime(3397): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.logincheck/com.example.logincheck.MainActivity}: java.lang.NullPointerException
05-21 04:13:02.284: E/AndroidRuntime(3397): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-21 04:13:02.284: E/AndroidRuntime(3397): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-21 04:13:02.284: E/AndroidRuntime(3397): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-21 04:13:02.284: E/AndroidRuntime(3397): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-21 04:13:02.284: E/AndroidRuntime(3397): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 04:13:02.284: E/AndroidRuntime(3397): at android.os.Looper.loop(Looper.java:137)
05-21 04:13:02.284: E/AndroidRuntime(3397): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-21 04:13:02.284: E/AndroidRuntime(3397): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 04:13:02.284: E/AndroidRuntime(3397): at java.lang.reflect.Method.invoke(Method.java:511)
05-21 04:13:02.284: E/AndroidRuntime(3397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-21 04:13:02.284: E/AndroidRuntime(3397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-21 04:13:02.284: E/AndroidRuntime(3397): at dalvik.system.NativeStart.main(Native Method)
05-21 04:13:02.284: E/AndroidRuntime(3397): Caused by: java.lang.NullPointerException
05-21 04:13:02.284: E/AndroidRuntime(3397): at com.example.logincheck.MainActivity.login(MainActivity.java:182)
05-21 04:13:02.284: E/AndroidRuntime(3397): at com.example.logincheck.MainActivity.onCreate(MainActivity.java:68)
05-21 04:13:02.284: E/AndroidRuntime(3397): at android.app.Activity.performCreate(Activity.java:5104)
05-21 04:13:02.284: E/AndroidRuntime(3397): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-21 04:13:02.284: E/AndroidRuntime(3397): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-21 04:13:02.284: E/AndroidRuntime(3397): ... 11 more

注意:我想要像我们在 Facebook 上拥有的东西。输入用户名、密码并选中记住我复选框后,如果我再次打开 Facebook 页面,那么我的主页将出现,而不是我必须单击登录按钮的欢迎屏幕。

最佳答案

在这里:

 dialog.dismiss();  //<<

saveLogin==true 时,您忘记在调用 dismiss() 之前初始化对话框。因此,如果 saveLogintrue,则在 if block 内初始化对话框,并在调用 dismiss( ) 对于 ProgressBar 。这样做:

if(null !=dialog){
if(dialog.isShowing()){
// dismiss progressDialog here
dialog.dismiss();
}
}

关于android - 登录屏幕内存功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16767689/

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