在我的onCreate函数中添加了ip部分检查后,我的移动应用程序停止工作,使注册用户绕过登录屏幕。这可能与变量声明有关,但我仍然不确定发生了什么。
private EditText etUsername;
private EditText etPassword;
private EditText etIpAddress;
private Button btnLogin;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
String username = etUsername.toString();
String password = etPassword.toString();
String ipAddress = etIpAddress.toString();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
if (sharedPreferences.contains("ip")) {
performLogin(username, password, sharedPreferences.getString("ip", ipAddress));
}
// declaring variebles
etUsername = (EditText)findViewById(R.id.etUsername);
etPassword= (EditText)findViewById(R.id.etPassword);
btnLogin = (Button)findViewById(R.id.btnLogin);
etIpAddress = (EditText) findViewById(R.id.etIpAddress);
// setting up things for login button
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ipAddress = etIpAddress.getText().toString();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
sharedPreferences.edit()
.putString("ip", ipAddress)
.apply();
String username = etUsername.getText().toString().trim();
String password = etPassword.getText().toString().trim();
performLogin(username, password, ipAddress);
}
});
}
private void performLogin(String username, String password, String ipAddress) {
try {
Device.login(username, password, ipAddress, this);
} catch (JSONException e) {
onLoginFailure(e);
}
}
logcat的:
06-06 16:40:11.408 4000-4000/com.itemlocator.findit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.itemlocator.findit, PID: 4000
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itemlocator.findit/com.itemlocator.findit.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.EditText.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.EditText.toString()' on a null object reference
at com.itemlocator.findit.LoginActivity.onCreate(LoginActivity.java:32)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
String username = etUsername.toString();
String password = etPassword.toString();
String ipAddress = etIpAddress.toString();
将生成NullPointerExceptions,因为您的EditTexts尚未初始化。你必须先打电话:
etUsername = (EditText)findViewById(R.id.etUsername);
etPassword= (EditText)findViewById(R.id.etPassword);
btnLogin = (Button)findViewById(R.id.btnLogin);
etIpAddress = (EditText) findViewById(R.id.etIpAddress);
我认为你的意思是
String username = etUsername.getText().toString();
答案 1 :(得分:0)
在登录屏幕中,在login_WS调用之后输入此代码
`SharedPreferences pref = getActivity()。getSharedPreferences(MyPre,Context.MODE_PRIVATE); 编辑器编辑器= pref.edit(); editor.putString(PARAMS.KEY_PROFILE_DATA,jsonObject.get(" data")。toString()); editor.putString(PARAMS.KEY_USER_ID,ConstantData.USER_ID); editor.putBoolean(PARAMS.KEY_STAY_LOGGED_IN,true); editor.commit();
在启动画面中输入此代码
`private Runnable mRunnable = new Runnable(){
@Override
public void run() {
pref = getSharedPreferences(MyPre, Context.MODE_PRIVATE);
//Log.i("splash","Boolen value 1" + pref.getBoolean(PARAMS.KEY_STAY_LOGGED_IN,false));
is = Boolean.parseBoolean(String.valueOf((pref.getBoolean(PARAMS.KEY_STAY_LOGGED_IN, false))));
Log.i("splash","Boolen value 2" + is);
if (is) {
Intent mIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(mIntent);
// overridePendingTransition(R.anim.enter, R.anim.no_anim);
finish();
} else {
Intent mIntent = new Intent(SplashScreenActivity.this, LoginActivity.class);
startActivity(mIntent);
overridePendingTransition(R.anim.enter, R.anim.no_anim);
finish();
}`
希望这对你有用