海傢伙問候所有... 新的Android字段..我創建新的Android應用程序使用登錄和註冊屏幕...我的問題是我想如果我的應用程序已登錄用戶已經goto菜單activty其他去登錄頁面在android ..請幫助我通過提供示例編碼或想法..感謝提前..如果應用程序已登錄已goto主要activty其他去登錄頁面android
這是我正在使用的代碼。對於我的QS,但其沒有工作..
package com.example.android_todist;
import java.util.logging.Handler;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class SplashActivity extends Activity {
Intent intent;
String userName,password;
SharedPreferences pref;
Editor editor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
pref = PreferenceManager.getDefaultSharedPreferences(this);
String storedUsername = pref.getString(userName, "");
String storedPassword = pref.getString(password, ""); //return nothing if no pass saved
if (!storedUsername .equalsIgnoreCase("") || !storedPassword .equalsIgnoreCase("")) {
Intent intent = new Intent(this, Todist1.class);
startActivity(intent);
finish();
}else {
Intent intent = new Intent(this, Login.class);
startActivity(intent);
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(intent);
finish();
}
}, 3000);
}
}
會重定向到主頁。但如果新用戶打開我的應用程序也將重定向到主頁着能夠獲得登錄頁面..
這是我的錯誤日誌..:
07-24 04:21:36.135: E/AndroidRuntime(1080): FATAL EXCEPTION: main
07-24 04:21:36.135: E/AndroidRuntime(1080): java.lang.NullPointerException
07-24 04:21:36.135: E/AndroidRuntime(1080): at
android.app.Instrumentation.execStartActivity(Instrumentation.java:1410)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
android.app.Activity.startActivityForResult(Activity.java:3370)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
android.app.Activity.startActivityForResult(Activity.java:3331)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
android.app.Activity.startActivity(Activity.java:3566)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
android.app.Activity.startActivity(Activity.java:3534)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
com.example.android_todist.SplashActivity$1.run(SplashActivity.java:50)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
android.os.Handler.handleCallback(Handler.java:725)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
android.os.Handler.dispatchMessage(Handler.java:92)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
android.os.Looper.loop(Looper.java:137)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
android.app.ActivityThread.main(ActivityThread.java:5041)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
java.lang.reflect.Method.invokeNative(Native Method)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
java.lang.reflect.Method.invoke(Method.java:511)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-24 04:21:36.135: E/AndroidRuntime(1080): at
dalvik.system.NativeStart.main(Native Method)
入住這http://stackoverflow.com/questions/18843625/how-do-i-use-shared-pref-file-for-my-apps-log-in-page/18843908#18843908 – ryderz8
請查看我的編碼... – Android
刪除'import java.util.logging.Handler;' – ryderz8