gpt4 book ai didi

java - 跳过登录 Activity 时获取 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 04:26:24 24 4
gpt4 key购买 nike

我想使用共享首选项跳过登录 Activity ,但每当我尝试实现此代码时,都会发生 java.lang.nullpointerexception 错误。这是启动或登录屏幕的 java 代码。这里我没有启动画面。

public class MainActivity extends Activity {
Button sub;
SharedPreferences prefs;
EditText useret,pwdet;
String IS_LOGIN = "IsLoggedIn";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(this.isLoggedIn()==true) {
checklogin();
}
setContentView(R.layout.activity_main);
useret=(EditText) findViewById(R.id.etuser);
pwdet=(EditText) findViewById(R.id.etpwd);
sub=(Button) findViewById(R.id.submit);
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username=useret.getText().toString();
String password=pwdet.getText().toString();
prefs=getSharedPreferences("codelearn_twitter", MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putString("key_for_username",username);
editor.putString("key_for_passwrd",password);
editor.putBoolean(IS_LOGIN, true);
editor.commit();

Intent i=new Intent(MainActivity.this,listactivity.class);
startActivity(i);
}
});
}
public void checklogin(){
if(this.isLoggedIn()==true){
Intent i=new Intent(MainActivity.this,listactivity.class);
startActivity(i);
finish();
}
}
public boolean isLoggedIn(){
return prefs.getBoolean(IS_LOGIN, false);
}
}

下面是错误列表..

08-19 17:58:17.061 14978-14978/com.example.viren.codelearn
E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.viren.codelearn, PID: 14978 java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.viren.codelearn/com.example.vire n.codelearn.MainActivity}:java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2305) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2363) at android.app.ActivityThread.access$900(ActivityThread.java:161) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1265) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5356) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1265) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.viren.codelearn.MainActivity.isLoggedIn (MainActivity.java:56) at com.example.viren.codelearn.MainActivity.onCreate (MainActivity.java:23) at android.app.Activity.performCreate(Activity.java:5426) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2269)  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2363)

   

最佳答案

查看你的堆栈跟踪,你可以看到在第 56 行你调用了 isLoggedIn ,它调用了你的 prefs 变量,该变量尚未初始化(它为空),因此你的获取空指针异常。

使 prefs 变量可供整个类访问,并在调用 isLoggedIn 之前对其进行初始化,或在 isLoggedIn 方法内对其进行初始化。

关于java - 跳过登录 Activity 时获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32096189/

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