0

我在我的 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

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

4

1 回答 1

0

你需要这样做:

SharedPreferences settings = mContext.getSharedPreferences("1", 0);
于 2013-09-05T01:14:25.610 回答