【问题标题】:Activity crashes when I include Shared Preferences当我包含共享首选项时,活动崩溃
【发布时间】:2019-10-26 17:39:52
【问题描述】:

我的活动运行良好,但是当我包含此代码时,它突然崩溃:

有人知道为什么会这样吗?

没有此代码,它不会崩溃。即使我不调用任何函数,它也会在活动打开后立即崩溃。我在这里缺少什么?

我使用的是最新版本的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, "");
    } 

【问题讨论】:

  • 我认为如果没有一个最小的可运行示例,很难找出这里出了什么问题。至少负责调用guardar、cagar的代码,以及调用findViewById的方法
  • 请包含控制台中出现的错误信息
  • 好的,我在里面包含了控制台的错误信息
  • 请粘贴完整代码、调用函数的方式等
  • 1.) 不要手动创建新的 Activity 实例,以及 2.) 您应该仅在 onCreate 或之后访问上下文内容,而不是直接访问字段

标签: java android android-studio-3.0 mobile-development


【解决方案1】:

你好,

根据我在大学中学到的知识和经验,您使用 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() 会更好(由于某种原因,编译器很少希望我同时使用两者)。

请注意:

如果您仍然希望使用您尝试的方式,可以参考以下答案:

请随时向我们发布您的问题解决方案,不要忘记接受答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    相关资源
    最近更新 更多