2012-09-28 6 views
1

SharedPreferences를 사용하여이 활동을 시작하면 충돌이 발생합니다. 먼저 활동 코드를 게시하고 LogCat을 게시합니다. 고마워요, 너희들 항상 큰 도움이된다! :-)Android : NullPointerException SharedPreferences 사용

활동 코드;

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입니다.

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 

답변

7

oncreate가 호출 된 후에 공유 환경 설정에 액세스해야합니다. 하지 OnCreate

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

()

+0

당신은 수상 자격이 있습니다. 수락 된 대답은 어떻게 들릴까요? :) (물론 6 분) –

+0

: D 감사합니다. 다행히 도움이되었습니다. –

1

이 설정 인스턴스의 인스턴스를 이동 : 그건 당신이 널 포인터 예외이 라인을

이동을 얻고있는 이유 :) 아니면 상황이 null이 될 것입니다 변수를 onCreate 메소드에 추가하십시오. super.onCreate 호출 후.

지금하고있는 방식대로 null로 설정됩니다.

1
public class MainActivity extends Activity{ 

SharedPreferences sharedpreferences;  

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

sharedpreferences = getSharedPreferences("preferences", Context.MODE_PRIVATE); 

      editor.putBoolean("sharedpreferences", true); 
      editor.commit(); 

// your code ... 

} 

// your code ... 
}