我是 android 新手,这个问题是一个转贴,因为某些原因我无法更新前一个问题。我可以在我的登录页面中创建记住我的功能,当我重新启动我的应用程序时,用户名和密码字段已经填写,但我必须单击登录按钮才能进入另一个活动。
如果我想直接加载另一个活动,如果记住我复选框为真,而不是在我再次加载我的应用程序时再次单击登录按钮,如何编码。
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());
}
}
我得到空指针异常。如果选中记住我框,我将无法切换到另一个活动。这是堆栈跟踪。
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 页面,则会出现我的主页,而不是我必须点击登录按钮的欢迎屏幕。