简体   繁体   中英

Activity crashes when I include Shared Preferences

My activity works fine but when I include this code it suddenly crashes:

Does anyone know why this happens?

Without this code it doesn't crash. It crashes as soon as the activity opens, even when i don't call any function. What I am missing here?

I am using the latest version of Android Studio, and the.xml file doesn't have any errors, but the IDE doesn't detect any error on this code neither, could it be that the module is too old or something?

Also the "Activity.java" have a ton of errors, but as I said before when i simply remove the code below it runs fine. I am probably just missing some command or something, does anyone know how to help me with this?

It gives me the next error:

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.

My code:

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, "");
    } 

Hello,

For what I've learn in college and in my experience, the way you used the SharedPreference is not the ideal way.

try this way of initiating and calling the SharedPreference object:

 SharedPreference sp = PreferenceManager.getDefaultSharedPreferences(this);
 text = sp.getString(TEXT, "");       //I actually prefer to put null as default instead of ""

your putBoolean looks fine, and don't forget to import everything that related to it.

 import android.content.SharedPreferences;
 import android.preference.PreferenceManager;
 //And everything else the android studio(or other IDE) tells you.

if still you have a problem try replacing .apply() with .commit() , but for most cases .apply() will be better(rarelyfor some reason the compiler wants me to use both).

Please Note:

If you still wish to use the way you're trying you can refer to this answers:

please keep us posted about your problem solution, and don't forget to accept the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM