我的活动正常,但是当我包含此代码时,它突然崩溃:
有人知道为什么会这样吗?
没有此代码,它不会崩溃。活动一打开,它就会崩溃,即使我没有调用任何函数也是如此。我在这里想念的是什么?
我正在使用最新版本的Android Studio,并且.xml文件没有任何错误,但是IDE也不在此代码上检测到任何错误,可能是模块太旧或其他原因?
此外,“ Activity.java”也有很多错误,但是正如我之前所说,当我简单地删除下面的代码时,它运行良好。我可能只是缺少一些命令或某些内容,有人知道如何帮助我吗?
它给我下一个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sltoolset, PID: 8778
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.sltoolset/com.example.sltoolset.Notas}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
at android.content.Context.obtainStyledAttributes(Context.java:738)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.example.sltoolset.Notas.<init>(Notas.java:26)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 8778 SIG: 9
Process 8778 terminated.
我的代码:
public static final String TEXT = "text";
private EditText edt1 = (EditText) findViewById(R.id.editText);
private String text;
public void guardar(View view)
{
SharedPreferences sp = getSharedPreferences(TEXT, MODE_PRIVATE);
SharedPreferences.Editor edt = sp.edit();
edt.putString(TEXT, edt1.getText().toString());
edt.apply();
Toast.makeText(this, "Se guardo el texto", Toast.LENGTH_SHORT).show();
}
public void cargar()
{
SharedPreferences sp = getSharedPreferences(TEXT, MODE_PRIVATE);
text = sp.getString(TEXT, "");
}
对于我在大学中所学的知识和我的经验,您使用SharedPreference的方式并不是理想的方法。
尝试这种方式来初始化和调用SharedPreference对象:
SharedPreference sp = PreferenceManager.getDefaultSharedPreferences(this);
text = sp.getString(TEXT, ""); //I actually prefer to put null as default instead of ""
您的putBoolean
看起来不错,请不要忘记导入与其相关的所有内容。
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
//And everything else the android studio(or other IDE) tells you.
[如果仍然有问题,请尝试将.apply()
替换为.commit()
,但在大多数情况下,.apply()
会更好(出于某些原因,编译器希望我同时使用两者)。
如果您仍然希望使用尝试的方式,可以参考以下答案:
请向我们发布您的问题解决方案,不要忘记接受答案。