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