Android:NullPointerException与SharedPreferences一起使用 [英] Android: NullPointerException Working With SharedPreferences

查看:51
本文介绍了Android:NullPointerException与SharedPreferences一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SharedPreferences,此活动在启动时崩溃.首先,我将发布活动代码,然后将我的LogCat发布.非常感谢你们,你们总是有很大的帮助!:-)

Working with SharedPreferences, this activity crashes upon launching. First I'll post the activity code, and then I'll post my LogCat. Thank you so much guys, you guys are always such big help! :-)

活动代码;

package com.creativecoders.gymbuddy;

import com.creativecoders.gymbuddy.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class Benchmark extends Activity {

public static final String GB_PREFERENCES_BENCH = "Bench";
public static final String GB_PREFERENCES_FLIES = "Flies";

SharedPreferences settings = getSharedPreferences("gBValues", 
     Context.MODE_PRIVATE);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_benchmark);
    }

public void onStart() {
    super.onStart();

    findViewById(R.id.button5).setOnClickListener(new handleButton5());

}

class handleButton5 implements OnClickListener {
    public void onClick(View v) {

        EditText editText1 = (EditText)findViewById(R.id.editText1);
        String sWeight = editText1.getText().toString();
        final double dWeight = Double.parseDouble(sWeight);

        EditText editText2 = (EditText)findViewById(R.id.editText2);
        String sPush = editText2.getText().toString();
        final double dPush = Double.parseDouble(sPush);

        EditText editText3 = (EditText)findViewById(R.id.editText3);
        String sSit = editText3.getText().toString();
        final double dSit = Double.parseDouble(sSit);

        EditText editText4 = (EditText)findViewById(R.id.editText4);
        String sPull = editText4.getText().toString();
        final double dPull = Double.parseDouble(sPull);

        double dBench = (((Math.floor(dWeight*.0664))*10)-10)+dPush;
        double dFlies = (Math.floor(((Math.floor(dBench*.6)/10)*10)));

        int iBench = (int)dBench;
        int iFlies = (int)dFlies;

        String sBench = "" + iBench;
        String sFlies = "" + iFlies;

        SharedPreferences.Editor editor1 = settings.edit();
        editor1.putString(GB_PREFERENCES_BENCH, sBench);
        editor1.commit();

        SharedPreferences.Editor editor2 = settings.edit();
        editor2.putString(GB_PREFERENCES_FLIES, sFlies);
        editor2.commit();

        TextView TextView1 = (TextView)findViewById(R.id.textView1);
        TextView1.setText(String.valueOf("Bench Press "+ iBench +" lbs"));

        TextView TextView2 = (TextView)findViewById(R.id.textView2);
        TextView2.setText(String.valueOf("Bar Curls "+ iCurls +" lbs"));
        }
    }

}

这是我的LogCat;

Here's my LogCat;

09-28 21:18:52.636: E/AndroidRuntime(686): FATAL EXCEPTION: main
09-28 21:18:52.636: E/AndroidRuntime(686): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.creativecoders.gymbuddy/com.creativecoders.gymbuddy.Benchmark}: java.lang.NullPointerException
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.os.Looper.loop(Looper.java:137)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread.main(ActivityThread.java:4340)
09-28 21:18:52.636: E/AndroidRuntime(686):  at java.lang.reflect.Method.invokeNative(Native Method)
09-28 21:18:52.636: E/AndroidRuntime(686):  at java.lang.reflect.Method.invoke(Method.java:511)
09-28 21:18:52.636: E/AndroidRuntime(686):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-28 21:18:52.636: E/AndroidRuntime(686):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-28 21:18:52.636: E/AndroidRuntime(686):  at dalvik.system.NativeStart.main(Native Method)
09-28 21:18:52.636: E/AndroidRuntime(686): Caused by: java.lang.NullPointerException
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:153)
09-28 21:18:52.636: E/AndroidRuntime(686):  at com.creativecoders.gymbuddy.Benchmark.<init>(Benchmark.java:35)
09-28 21:18:52.636: E/AndroidRuntime(686):  at java.lang.Class.newInstanceImpl(Native Method)
09-28 21:18:52.636: E/AndroidRuntime(686):  at java.lang.Class.newInstance(Class.java:1319)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
09-28 21:18:52.636: E/AndroidRuntime(686):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
09-28 21:18:52.636: E/AndroidRuntime(686):  ... 11 more

推荐答案

在调用onCreate之后,您必须访问共享首选项.否则上下文将为null :)这就是为什么您会得到null指针异常

You have to access the shared preferences AFTER onCreate is called. Or else the context would be null :) that's why you're getting a null pointer exception

移动此行:

SharedPreferences settings = getSharedPreferences("gBValues", 
     Context.MODE_PRIVATE);

在onCreate()

in the onCreate()

这篇关于Android:NullPointerException与SharedPreferences一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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