NullPointerException异常的共享preferences声明Android应用? [英] NullPointerException in android app on sharedpreferences declaration?

查看:219
本文介绍了NullPointerException异常的共享preferences声明Android应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,做一个应用程序。我添加新的code加载,而现在FC的开放:(

So, making an app. I added loads of new code, and now it FC's on open :(

下面是logcat的:

Here is the logcat:

07-25 16:59:12.490: D/AndroidRuntime(26121): Shutting down VM
07-25 16:59:12.495: W/dalvikvm(26121): threadid=1: thread exiting with uncaught exception (group=0x40a641f8)
07-25 16:59:12.495: E/AndroidRuntime(26121): FATAL EXCEPTION: main
07-25 16:59:12.495: E/AndroidRuntime(26121): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.liamwli.smsbusy/com.liamwli.smsbusy.Sms_busyActivity}: java.lang.NullPointerException
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1993)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.app.ActivityThread.access$600(ActivityThread.java:132)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.os.Looper.loop(Looper.java:137)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.app.ActivityThread.main(ActivityThread.java:4575)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at java.lang.reflect.Method.invokeNative(Native Method)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at java.lang.reflect.Method.invoke(Method.java:511)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at dalvik.system.NativeStart.main(Native Method)
07-25 16:59:12.495: E/AndroidRuntime(26121): Caused by: java.lang.NullPointerException
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:371)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at com.liamwli.smsbusy.Sms_busyActivity.<init>(Sms_busyActivity.java:23)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at java.lang.Class.newInstanceImpl(Native Method)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at java.lang.Class.newInstance(Class.java:1319)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
07-25 16:59:12.495: E/AndroidRuntime(26121):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1984)
07-25 16:59:12.495: E/AndroidRuntime(26121):    ... 11 more
07-25 16:59:12.940: I/dalvikvm(26121): threadid=3: reacting to signal 3
07-25 16:59:12.950: I/dalvikvm(26121): Wrote stack traces to '/data/anr/traces.txt'
07-25 16:59:13.040: I/dalvikvm(26121): threadid=3: reacting to signal 3
07-25 16:59:13.045: I/dalvikvm(26121): Wrote stack traces to '/data/anr/traces.txt'

Sms_busyActivity.java的第23行是:

Line 23 of Sms_busyActivity.java is:

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

我不明白我收到此错误。

I don't understand what I am getting this error.

活动的其余低于(这是相当大的):

The rest of the activity is below (It is quite large):

package com.liamwli.smsbusy;

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.content.IntentFilter;
import android.content.SharedPreferences;

public class Sms_busyActivity extends Activity {
    IntentFilter intentFilter;
    ToggleButton endis;
    EditText message;
    Button smessage;
    SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    SharedPreferences.Editor editor = getPrefs.edit();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Intent i = new Intent("com.liamwli.smsbusy.PREFS");
        // startActivity(i);
        setContentView(R.layout.main);

        endis = (ToggleButton) findViewById(R.id.enableddis);

        smessage = (Button) findViewById(R.id.savemess);

        message = (EditText) findViewById(R.id.message);

        // ---intent to filter for SMS messages received---
        intentFilter = new IntentFilter();
        intentFilter.addAction("SMS_RECEIVED_ACTION");

        endis.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                Toast.makeText(Sms_busyActivity.this, "App state changed",
                        Toast.LENGTH_LONG).show();
                if (endis.isChecked()){
                    editor.putBoolean("enabled", true);
                    editor.commit();
                }else {
                    editor.putBoolean("enabled", false);
                    editor.commit();
                }

                editor.putString("message", message.getText().toString());
                editor.commit();

            }
        });

        smessage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                editor.putString("message", message.getText().toString());
                editor.commit();

            }
        });

    }

    @SuppressWarnings("deprecation")
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
                && keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            Log.d("CDA", "onKeyDown Called");
            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }

    public void onBackPressed() {
        Log.d("CDA", "onBackPressed Called");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);
    }

}

请有人可以帮我吗?

推荐答案

移动下面的code到的onCreate()方法中。

Move the following code to within the onCreate() method.

getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

其原因是,在现有的code,您使用的上下文( getBaseContext())之前,它可以通过调用完全创建 super.onCreate()

The reason being, in your existing code, you're using the context (getBaseContext()) before it could be fully created by a call to super.onCreate().

这篇关于NullPointerException异常的共享preferences声明Android应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆