使用SharedPreferences会将应用程序与java.lang.NullPointerException崩溃

时间:2013-09-05 00:46:20

标签: java android sharedpreferences

我在Android应用程序中遇到问题,其中SharedPreferences崩溃了应用程序。我正在创建一个货币转换器应用程序,所以我创建了一个SharedPreferences,以便在没有互联网连接时保存该值。我已经在谷歌搜索了一个星期,但我找不到答案。这是我的代码:

if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR")
                && edittextdollars.length() > 0
                && edittexteuros.length() == 0) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {

                    try {
                        handler.sendEmptyMessage(2);
                        String hello = edittextdollars.getText().toString();
                        double hello3 = Double.parseDouble(hello);
                        //getGraph("USD", "EUR");
                        convertvalues("USD", "EUR");
                        String hello2 = edittexteuros.getText().toString();
                        double hello4 = Double.parseDouble(hello2);
                        textview2.setText(String.valueOf(hello4 / hello3));
                        SharedPreferences settings = getSharedPreferences("1", 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString("eurusd", textview2.getText().toString());
                        editor.commit();
                    } catch (Exception e) {

                    }

                }
            });
            thread.start();

        }

@SuppressLint("HandlerLeak")
    public Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            dialog1.dismiss();
            try {
                switch (msg.what) {
                case 2:
                    try {
                    String hello = edittextdollars.getText().toString();
                    double hello3 = Double.parseDouble(hello);
                    //getGraph("USD", "EUR");
                    convertvalues("USD", "EUR");
                    String hello2 = edittexteuros.getText().toString();
                    double hello4 = Double.parseDouble(hello2);
                    textview2.setText(String.valueOf(hello4 / hello3));
                    } catch (Exception e) {
                    SharedPreferences settings = getSharedPreferences("1", 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("eurusd", j);
                    editor.commit(); 
                    //textview2.setVisibility(View.INVISIBLE);
                    }
                    break;

我唯一困惑的是SharedPreferences部分。

请忽略
                    handler.sendEmptyMessage(2);
                    String hello = edittextdollars.getText().toString();
                    double hello3 = Double.parseDouble(hello);
                    //getGraph("USD", "EUR");
                    convertvalues("USD", "EUR");
                    String hello2 = edittexteuros.getText().toString();
                    double hello4 = Double.parseDouble(hello2);
                    textview2.setText(String.valueOf(hello4 / hello3));

这很好。现在,当我添加这个......

SharedPreferences settings = getSharedPreferences("1", 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("eurusd", j);
                    editor.commit(); 

这就是我撞墙的地方。这有什么不对?为了以防万一,我将发布LogCat。

09-04 22:06:57.088: E/AndroidRuntime(887): FATAL EXCEPTION: main
09-04 22:06:57.088: E/AndroidRuntime(887): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.currencyconverter/com.example.currencyconverter.MainActivity2}: java.lang.NullPointerException
09-04 22:06:57.088: E/AndroidRuntime(887):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
09-04 22:06:57.088: E/AndroidRuntime(887):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-04 22:06:57.088: E/AndroidRuntime(887):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-04 22:06:57.088: E/AndroidRuntime(887):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-04 22:06:57.088: E/AndroidRuntime(887):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 22:06:57.088: E/AndroidRuntime(887):  at android.os.Looper.loop(Looper.java:137)
09-04 22:06:57.088: E/AndroidRuntime(887):  at android.app.ActivityThread.main(ActivityThread.java:5041)
09-04 22:06:57.088: E/AndroidRuntime(887):  at java.lang.reflect.Method.invokeNative(Native Method)
09-04 22:06:57.088: E/AndroidRuntime(887):  at java.lang.reflect.Method.invoke(Method.java:511)
09-04 22:06:57.088: E/AndroidRuntime(887):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-04 22:06:57.088: E/AndroidRuntime(887):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
09-04 22:06:57.088: E/AndroidRuntime(887):  at dalvik.system.NativeStart.main(Native Method)
09-04 22:06:57.088: E/AndroidRuntime(887): Caused by: java.lang.NullPointerException
09-04 22:06:57.088: E/AndroidRuntime(887):  at com.example.currencyconverter.MainActivity2.<init>(MainActivity2.java:102)
09-04 22:06:57.088: E/AndroidRuntime(887):  at java.lang.Class.newInstanceImpl(Native Method)
09-04 22:06:57.088: E/AndroidRuntime(887):  at java.lang.Class.newInstance(Class.java:1319)
09-04 22:06:57.088: E/AndroidRuntime(887):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
09-04 22:06:57.088: E/AndroidRuntime(887):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
09-04 22:06:57.088: E/AndroidRuntime(887):  ... 11 more

提前感谢您考虑我的问题。

1 个答案:

答案 0 :(得分:0)

你需要这样做:

SharedPreferences settings = mContext.getSharedPreferences("1", 0);