I can't use sharedpreferences without android app crashing - android

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
float highscore = pref.getFloat("highscore",0.0f);
As soon as I enter the activity that uses this part, the app crashes. According to logcat, that first line in this code block gives a nullpointerException. If it's important, this part comes after OnCreate, and I'm using Android Studio.
Logcat:FATAL EXCEPTION: main
Process: com.nonexistent.rs.guessthenumber, PID: 4388
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nonexistent.rs.guessthenumber/com.nonexistent.rs.guessthenumber.main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:185)
at com.nonexistent.rs.guessthenumber.main.<init>(main.java:141)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
at android.app.ActivityThread.access$900(ActivityThread.java:175) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5602) 
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:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method) 
07-14 10:17:04.616 6030-6030/com.nonexistent.rs.guessthenumber E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nonexistent.rs.guessthenumber, PID: 6030
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nonexistent.rs.guessthenumber/com.nonexistent.rs.guessthenumber.main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:185)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
at com.nonexistent.rs.guessthenumber.main.<init>(main.java:142)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
at android.app.ActivityThread.access$900(ActivityThread.java:175) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5602) 
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:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method) 

Reason:
May be you are using this code from a fragment or on other thread. You Need to pass a activity context reference as an argument. The argument this is the problem.
Solution:
You need to pass the activity / context as the argument
SharedPreferences pref =
PreferenceManager.getDefaultSharedPreferences(Your_Activity_Name.this);

I solved this myself a long time ago, but then I realized that this was left unanswered. Apparently, I'm supposed to put all SharedPrefences code in onCreate.

Related

E/AndroidRuntime: FATAL EXCEPTION: main PID: 5112 [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Hy
I am new to Android / Java is I am facing a BMO problem
I code it but it does not work
an idea ??
thank you
<string name="onCreate_Go">onCreate Go</string>
<TextView
android:id="#+id/scrollTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
import android.widget.TextView;
private final TextView txt = (TextView) this.findViewById(R.id.scrollTxt);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt.setText(R.string.onCreate_Go);
}
5112-5112/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gmail.test1, PID: 5112
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gmail.test1/com.gmail.test1.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.gmail.test1.MainActivity.(MainActivity.java:13)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
at android.app.ActivityThread.access$800(ActivityThread.java:135) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5017) 
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:779) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
at dalvik.system.NativeStart.main(Native Method) 
02-16 16:41:21.634 5112-5112/? I/Process: Sending signal. PID: 5112 SIG: 9
Please add your Activity in your AndroidManifest.xml. like below code:
<activity android:name=".activities.MainActivity"/>
Also put the findViewById() after setContentView() so it can actually return something other than null.
You can't initialize the textView before call "setContentView" method. I recommend you create a "init" method where initialize all layout's elements and call it after "setContentView"

Android error when BT is connect [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am a begginer in Android. I am a PLC programmer, but I would like to write application for my mobile phone. I would like to remote control RGB led via Arduino. But when I make any changes I have the error, and I don't know what is this.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.led.led, PID: 7790
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.led.led/com.led.led.ledControl}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.led.led.ledControl.onCreate(ledControl.java:121)
at android.app.Activity.performCreate(Activity.java:5458)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470) 
at android.app.ActivityThread.access$900(ActivityThread.java:174) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5593) 
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:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method)
A NullPointerException occurs when you try to access something not yet defined.
Like for eg. trying to setText in a textview before assigning it in the activity with the id in the xml.

Exception in Release Mode due to ZipFile

I'm using a code snippet to access a Zip File containing APK file . It works fine in Debug Mode, but, throws Exception in Release Mode.
ZipFile zip = new ZipFile(file);
ZipEntry mft = zip.getEntry("AndroidManifest.xml");
is = zip.getInputStream(mft);
It doesn't make any issue if released in Debug Mode. But, in Release Mode, app gets crashed.
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x418c6c08)
************ 29072016 ************
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vxeed.xnap/com.vxeed.xnap.forms.MainMenu}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.vxeed.xnap.common.ay.a(Unknown Source)
at com.vxeed.xnap.forms.MainMenu.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
at android.app.ActivityThread.access$900(ActivityThread.java:175) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5602) 
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:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method) 
If I get rid of this method, it works fine. Do I need to add anything extra in proguard-rules? Please advise. Thanks in advance

NavigationView NotSerializableException while pushing fragment

I am getting NotSerializableException when pushing one of my fragments in my app. I am using navigation view in my activity for side menu.
Caused by: java.io.NotSerializableException: android.support.design.widget.NavigationView
I have a ProfileFragment in my MainActivity. I am re using my EntriesList fragment inside ProfileFragment. On clicking an item in EntriesList, the EntryDetailsFragment is being pushed to main container. This is crashing the app with above message. I have included complete stack trace below.
FATAL EXCEPTION: main
Process: com.ajith.trivado, PID: 5597
java.lang.RuntimeException: Parcelable encountered IOException writing
serializable object (name = com.ajith.trivado.activities.MainActivity)
at android.os.Parcel.writeSerializable(Parcel.java:1316)
at android.os.Parcel.writeValue(Parcel.java:1264)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
at android.os.Bundle.writeToParcel(Bundle.java:1692)
at android.os.Parcel.writeBundle(Parcel.java:636)
at android.app.FragmentState.writeToParcel(Fragment.java:132)
at android.os.Parcel.writeTypedArray(Parcel.java:1133)
at
android.app.FragmentManagerState.writeToParcel(FragmentManager.java:373)
at android.os.Parcel.writeParcelable(Parcel.java:1285)
at android.os.Parcel.writeValue(Parcel.java:1204)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
at android.os.Bundle.writeToParcel(Bundle.java:1692)
at android.os.Parcel.writeBundle(Parcel.java:636)
at
android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2506)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3165)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.NotSerializableException:
android.support.design.widget.NavigationView
at
java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
at
java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at
java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at
java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at
java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
at
java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368)
at
java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
at
java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
at
java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
at
java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
at
java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
at android.os.Parcel.writeSerializable(Parcel.java:1311)
at android.os.Parcel.writeValue(Parcel.java:1264) 
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618) 
at android.os.Bundle.writeToParcel(Bundle.java:1692) 
at android.os.Parcel.writeBundle(Parcel.java:636) 
at android.app.FragmentState.writeToParcel(Fragment.java:132) 
at android.os.Parcel.writeTypedArray(Parcel.java:1133) 
at
android.app.FragmentManagerState.writeToParcel(FragmentManager.java:373) 
at android.os.Parcel.writeParcelable(Parcel.java:1285) 
at android.os.Parcel.writeValue(Parcel.java:1204) 
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618) 
at android.os.Bundle.writeToParcel(Bundle.java:1692) 
at android.os.Parcel.writeBundle(Parcel.java:636) 
at
android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2506) 
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3165) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5086) 
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:785) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
at dalvik.system.NativeStart.main(Native Method)
I should see the code but I think you are sending some extra data to your fragment. The class of your object that is being sent should implement Serializable.

NoClassDefFoundError when Sending email wiith the library: mysql-connector-java-5.0.8-bin

I have an issue in my android application when I try to send an email. If I use the library for send mail(mail.jar, activation.jar, additionnal.jar) and connector for android: mysql-connector-java-5.0.8-bin.jar throws an error:
java.lang.NoClassDefFoundError: com.example.timis.util.sendMails.Sendmail$1
at com.example.timis.util.sendMails.Sendmail.sendMail(Sendmail.java:31)
at com.example.timis.tour.LoginActivity.onCreate(LoginActivity.java:189)
at android.app.Activity.performCreate(Activity.java:5458)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5593)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
If I remove mysql-connector-java-5.0.8-bin it works well.

Categories

Resources