试图引用空对象引用?

时间:2016-05-05 01:15:35

标签: java android

所以我一直在制作一个简单的应用程序来静音和取消静音相机,是的,我知道这个应用程序在许多国家都是非法的,但是!我愿意尝试。无论如何,如果事情变得粗糙,免责声明就会存在。

在开发过程中,我在尝试调用SharedPreferences时遇到了这个问题。这就是我的代码现在的制定方式。

  • MainActivity.java,初始化所有内容,然后调用...
    • LegalProsecution.java,如果设置了first_run,它会尝试警告用户。
      • AppPreferences.java处理给LP.java提供first_run状态。

所以,我一直遇到AppPreferences.java的问题。这是代码:

package ideaman924.camerasilencer;

import android.content.Context;
import android.content.SharedPreferences;

public class AppPreference
{
    SharedPreferences prefs;

    public AppPreference(String buffer, Context context)
    {
        prefs = context.getSharedPreferences("ideaman924.camerasilencer.first_run", Context.MODE_PRIVATE);
    }

    public void storeSettings(String buffer, int num)
    {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(buffer, num);
        editor.apply();
    }

    public int loadSettings(String buffer)
    {
        return prefs.getInt(buffer,0);
    }
}

这条线给了我最后通::

        prefs = context.getSharedPreferences("ideaman924.camerasilencer.first_run", Context.MODE_PRIVATE);

以下是来自logcat的崩溃日志:

05-05 10:10:33.233 9099-9099/ideaman924.camerasilencer E/AndroidRuntime: FATAL EXCEPTION: main
Process: ideaman924.camerasilencer, PID: 9099
Theme: themes:{}
java.lang.RuntimeException: Unable to start activity ComponentInfo{ideaman924.camerasilencer/ideaman924.camerasilencer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5458)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at ideaman924.camerasilencer.AppPreference.<init>(AppPreference.java:12)
at ideaman924.camerasilencer.LegalProsecution.<init>(LegalProsecution.java:11)
at ideaman924.camerasilencer.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5458) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

根据我的猜测,SharedPreferences prefs会产生一个空对象,我试图引用它。但为什么?为什么它是一个空对象?而且,我尝试初始化prefs,而不是引用它!

任何帮助都将不胜感激。

EDIT1:似乎错误发生在LegalProsecution.java,如cricket_007所述:

package ideaman924.camerasilencer;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;

public class LegalProsecution
{
    private Context context;
    public LegalProsecution(Context context)
    {
        this.context = context;
    }
    AppPreference appprefs = new AppPreference("settings",this.context);
    public void warningShow()
    {
        if(appprefs.loadSettings("first_run") != 1) {
            AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
            builder1.setTitle(context.getResources().getString(R.string.warning));
            builder1.setMessage(context.getResources().getString(R.string.warning_description));
            builder1.setCancelable(true);

            builder1.setPositiveButton(
                    context.getResources().getString(R.string.yes),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //Got promise from user, now setting first_run to 1
                            appprefs.storeSettings("first_run", 1);
                            dialog.cancel();
                        }
                    }
            );
            builder1.setNegativeButton(
                    context.getResources().getString(R.string.no),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //Okay, cool, bye! No CS for you!
                            dialog.cancel();
                            ((Activity) context).finish();
                            System.exit(0);
                        }
                    }
            );
            AlertDialog alert1 = builder1.create();
            alert1.show();
        }
    }
}

有什么问题吗?

1 个答案:

答案 0 :(得分:1)

像这样改写

AppPreference appprefs;

public LegalProsecution(Context context)
{
    this.context = context;
    this.appprefs = new AppPreference("settings", context);
}

因为this.context在调用构造函数之前将为null。