jinwook1469 / acra

Automatically exported from code.google.com/p/acra
0 stars 0 forks source link

NullPointerException on SharedPreferences(...).edit().putBoolean("acra.enable", false).commit(); #34

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I got a NullPointerException when executing 
SharedPreferences(...).edit().putBoolean("acra.enable", false).commit();

This is particularly the case when using it as Preference like 
<CheckBoxPreference android:key="acra.enable"
       android:title="@string/pref_enable_acra"
       android:summaryOn="@string/pref_acra_enabled"
       android:summaryOff="@string/pref_acra_disabled"
       android:defaultValue="false"/>

STACKTRACE

 java.lang.NullPointerException
     at org.acra.ErrorReporter.disable(ErrorReporter.java:888)
     at org.acra.ACRA$1.onSharedPreferenceChanged(ACRA.java:137)
     at android.app.ContextImpl$SharedPreferencesImpl$EditorImpl.commit(ContextImpl.java:2869)
     at android.preference.Preference.tryCommit(Preference.java:1198)
     at android.preference.Preference.persistBoolean(Preference.java:1403)
     at android.preference.CheckBoxPreference.setChecked(CheckBoxPreference.java:154)
     at android.preference.CheckBoxPreference.onClick(CheckBoxPreference.java:143)
     at android.preference.Preference.performClick(Preference.java:810)
     at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:198)
     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
     at android.widget.ListView.performItemClick(ListView.java:3382)
     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
     at android.os.Handler.handleCallback(Handler.java:587)
     at android.os.Handler.dispatchMessage(Handler.java:92)
     at android.os.Looper.loop(Looper.java:144)
     at android.app.ActivityThread.main(ActivityThread.java:4937)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     at dalvik.system.NativeStart.main(Native Method)

Original issue reported on code.google.com by lueghau...@gmail.com on 9 Feb 2011 at 12:44

GoogleCodeExporter commented 8 years ago
Well, it doesn't crash for the "acra.disable" settings. That's very weird, 
because I had a look into the files ACRA.java and ErrorReporter.java, and I 
don't see the cause. 

Original comment by lueghau...@gmail.com on 9 Feb 2011 at 1:12

GoogleCodeExporter commented 8 years ago
Where is your call to ACRA.init(Application) placed exactly ?

I see there could be in issue in some cases because the 
OnSharedPreferenceChangeListener is just before really intializing the 
ErrorReporter.

So if a preference change occurs just before the init, the applicationContext 
might not have been provided to the ErrorReporter... => NPE.

I've not been able to reproduce the crash, so the details of your 
Application.onCreate() method could help me.

Anyway, I will fix the init sequence so that the listener is set AFTER the 
ErrorReporter init... and add a test on the context to prevent any other case 
of NPE on this log entry.

I still haven't decided if I will release a fixed package for this or just 
include it in the incoming features release.

Original comment by kevin.gaudin on 12 Feb 2011 at 2:07

GoogleCodeExporter commented 8 years ago
I had it at two places: 

1)  In the onCreate() method, I changed the "acra.enable" prefences like 
    if (beta)
        prefs.edit().putBoolean("acra.enable", true).commit();
    else if (!prefs.contains("acra.enable"))
        // Disable it by default
        prefs.edit().putBoolean("acra.enable", false).commit();

    The last line produced the crash. 

2)  I placed it simply as CheckBoxPreference in the preferences xml layout
    <CheckBoxPreference android:key="acra.enable"
       android:title="@string/pref_enable_acra"
       android:summaryOn="@string/pref_acra_enabled"
       android:summaryOff="@string/pref_acra_disabled"
       android:defaultValue="false"/>

    Unchecking the CheckBoxPreference crashed the application.

Original comment by lueghau...@gmail.com on 12 Feb 2011 at 2:19

GoogleCodeExporter commented 8 years ago
Please send me the content of your Application.onCreate() method (where 
ACRA.init() is called) kevin.gaudin@gmail.com.

Thanks

Original comment by kevin.gaudin on 12 Feb 2011 at 2:22

GoogleCodeExporter commented 8 years ago
package com.example.app;

import org.acra.ACRA;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;

import android.app.Application;

@ReportsCrashes(formKey="",
                formUri="http://www.example.com/report.php",
                mode = ReportingInteractionMode.TOAST,
                resToastText = R.string.crash_report_sent,
                resNotifTickerText = R.string.crash_notif_ticker_text,
                resNotifTitle = R.string.crash_notif_title,
                resNotifText = R.string.crash_notif_text,
                resNotifIcon = android.R.drawable.stat_notify_error, // optional. default is a warning sign
                resDialogText = R.string.crash_dialog_text,
                resDialogIcon = android.R.drawable.ic_dialog_info, //optional. default is a warning sign
                resDialogTitle = R.string.crash_dialog_title, // optional. default is your application name
                resDialogCommentPrompt = R.string.crash_dialog_comment_prompt, // optional. when defined, adds a user text field input with this text resource as a label
                resDialogOkToast = R.string.crash_dialog_ok_toast // optional. displays a Toast message when the user accepts to send a report.
                )

public class MyApplication extends Application {

    public static final boolean BETA_VERSION = true;

    @Override
    public void onCreate() {
        // The following line triggers the initialization of ACRA
        ACRA.init(this);
        super.onCreate();
    }
}

Original comment by lueghau...@gmail.com on 12 Feb 2011 at 3:00

GoogleCodeExporter commented 8 years ago
Nothing wrong here. If you are able to reproduce the crash anytime, I could 
send you a new .jar and let you check that my fix works. Would that be ok for 
you ?

Original comment by kevin.gaudin on 12 Feb 2011 at 4:22

GoogleCodeExporter commented 8 years ago

Original comment by kevin.gaudin on 17 Feb 2011 at 6:56

GoogleCodeExporter commented 8 years ago
Just released a 3.1.1 version. See Downloads page. Please check that the fix 
works.

Original comment by kevin.gaudin on 18 Feb 2011 at 1:40

GoogleCodeExporter commented 8 years ago
I have the last version 4.2.3 "Stable". 
I found out that to prevent ACRA from crashing you have to initializate 
ErrorReporter first:

@Override
public void onCreate() {
    ErrorReporter.getInstance().init(this);
    ACRA.init(this); // The following line triggers the initialization of ACRA  
}

With this ACRA stop producing NullPointerException type of exceptions.
I don't know if this would change the behavior of ACRA or not. I wish there 
would be a test method that allow the developer to check is ACRA is working 
propertly or not.

Original comment by david.w....@gmail.com on 31 Oct 2011 at 8:30

GoogleCodeExporter commented 8 years ago

Original comment by kevin.gaudin on 22 Dec 2011 at 9:06