2014-09-23 22 views
0

我試圖讓某些複選框的檢查決定運行哪個公式。該課程的代碼如下。問題中的代碼是註釋掉的代碼。確定哪個公式運行的複選框

package com.mikitz.rogsimple; 

import org.apache.commons.lang3.math.NumberUtils; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.TextView; 

public class SaACharisma extends Activity { 


EditText charisma, persuasion, elicitation, animalhandling; 
TextView charismafpvalue, persuasionfpvalue, elicitationfpvalue, animalhandlingfpvalue; 
TextView fpr; 
CheckBox timid, sanguine; 

SharedPreferences pref; 

String getcharisma, getpersuasion, getelicitation, getanimalhandling; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.saa_charisma); 
    pref = getSharedPreferences("Character1", Context.MODE_PRIVATE); 

    fpr = (TextView) findViewById(R.id.fpr); 

    pref.getBoolean("timid", false); 
    pref.getBoolean("sanguine", false); 

    charismafpvalue = (TextView) findViewById(R.id.charismafpvalue); 
    persuasionfpvalue = (TextView) findViewById(R.id.persuasionfpvalue); 
    elicitationfpvalue = (TextView) findViewById(R.id.elicitationfpvalue); 
    animalhandlingfpvalue = (TextView) findViewById(R.id.animalhandlingfpvalue); 

    charisma = (EditText) findViewById(R.id.charisma); 
    persuasion = (EditText) findViewById(R.id.persuasion); 
    elicitation = (EditText) findViewById(R.id.elicitation); 
    animalhandling = (EditText) findViewById(R.id.animalhandling); 

    getcharisma = pref.getString("charisma", ""); 
    getpersuasion = pref.getString("persuasion", ""); 
    getelicitation = pref.getString("elicitation", ""); 
    getanimalhandling = pref.getString("animalhandling", ""); 

    charisma.setText(getcharisma); 
    persuasion.setText(getpersuasion); 
    elicitation.setText(getelicitation); 
    animalhandling.setText(getanimalhandling); 

    float CHRV = NumberUtils.toFloat(charisma.getText().toString(), 0.0f); 
    charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV)); 
    float PERV = NumberUtils.toFloat(persuasion.getText().toString(), 0.0f); 
    persuasionfpvalue.setText(Float.toString((PERV + 1) * PERV)); 
    float ELIV = NumberUtils.toFloat(elicitation.getText().toString(), 0.0f); 
    elicitationfpvalue.setText(Float.toString((ELIV + 1) * ELIV)); 
    float AMLV = NumberUtils.toFloat(animalhandling.getText().toString(), 0.0f); 
    animalhandlingfpvalue.setText(Float.toString((AMLV + 1) * AMLV)); 

//  if (timid.isChecked()) { 
//  charismafpvalue.setText(Float.toString(((CHRV + 1) * CHRV) * 2)); 
//  } else { 
//  charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV)); 
//  } 
//  if (sanguine.isChecked()) { 
//  charismafpvalue.setText(Float.toString(((CHRV + 1) * CHRV)/2)); 
//  } else { 
//  charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV)); 
//  } 

    float FP = NumberUtils.toFloat(pref.getString("fp", "")); 
    float HLTH = NumberUtils.toFloat(pref.getString("healthfpvalue", "")); 
    float CHR = NumberUtils.toFloat(charismafpvalue.getText().toString(), 0.0f); 
    float PER = NumberUtils.toFloat(persuasionfpvalue.getText().toString(), 0.0f); 
    float ELI = NumberUtils.toFloat(elicitationfpvalue.getText().toString(), 0.0f); 
    float AML = NumberUtils.toFloat(animalhandlingfpvalue.getText().toString(), 0.0f); 
    float MAN = NumberUtils.toFloat(pref.getString("manipulationfpvalue", "")); 
    float ACT = NumberUtils.toFloat(pref.getString("actingfpvalue", "")); 
    float SED = NumberUtils.toFloat(pref.getString("seductionfpvalue", "")); 
    float INTM = NumberUtils.toFloat(pref.getString("intimidationfpvalue", "")); 
    float INT = NumberUtils.toFloat(pref.getString("intelligencefpvalue", "")); 
    float TRP = NumberUtils.toFloat(pref.getString("trappingfpvalue", "")); 
    float FOR = NumberUtils.toFloat(pref.getString("forgingfpvalue", "")); 
    float CON = NumberUtils.toFloat(pref.getString("concoctionsfpvalue", "")); 
    float DEX = NumberUtils.toFloat(pref.getString("dexterityfpvalue", "")); 
    float ACR = NumberUtils.toFloat(pref.getString("acrobaticsfpvalue", "")); 
    float DOD = NumberUtils.toFloat(pref.getString("dodgingfpvalue", "")); 
    float RC = NumberUtils.toFloat(pref.getString("rangedfpvalue", "")); 
    float STR = NumberUtils.toFloat(pref.getString("strengthfpvalue", "")); 
    float MEL = NumberUtils.toFloat(pref.getString("meleefpvalue", "")); 
    float H2H = NumberUtils.toFloat(pref.getString("h2hfpvalue", "")); 
    float PRY = NumberUtils.toFloat(pref.getString("pryfpvalue", "")); 
    float WIT = NumberUtils.toFloat(pref.getString("witsfpvalue", "")); 
    float STL = NumberUtils.toFloat(pref.getString("stealthfpvalue", "")); 
    float WILD = NumberUtils.toFloat(pref.getString("wildernessfpvalue", "")); 
    float TRA = NumberUtils.toFloat(pref.getString("trackingfpvalue", "")); 
    float QI = NumberUtils.toFloat(pref.getString("qifpvalue", "")); 
    float FIRE = NumberUtils.toFloat(pref.getString("firefpvalue", "")); 
    float WAT = NumberUtils.toFloat(pref.getString("waterfpvalue", "")); 
    float EAR = NumberUtils.toFloat(pref.getString("earthfpvalue", "")); 
    float AIRV = NumberUtils.toFloat(pref.getString("airfpvalue", "")); 

    fpr.setText(Float.toString(FP - (QI + FIRE + WAT + EAR + AIRV + WIT + STL + WILD + TRA + STR + MEL + H2H + 
      PRY + MAN + ACT + SED + INTM + INT + TRP + FOR + CON + HLTH + CHR + PER + ELI + AML + DEX + ACR + DOD + RC))); 

// TEXT WATCHERS 
    TextWatcher watcher = new TextWatcher() { 
     public void afterTextChanged(Editable s) { 

      float CHRV = NumberUtils.toFloat(charisma.getText().toString(), 0.0f); 
      charismafpvalue.setText(Float.toString((CHRV + 1) * CHRV)); 
      float PERV = NumberUtils.toFloat(persuasion.getText().toString(), 0.0f); 
      persuasionfpvalue.setText(Float.toString((PERV + 1) * PERV)); 
      float ELIV = NumberUtils.toFloat(elicitation.getText().toString(), 0.0f); 
      elicitationfpvalue.setText(Float.toString((ELIV + 1) * ELIV)); 
      float AMLV = NumberUtils.toFloat(animalhandling.getText().toString(), 0.0f); 
      animalhandlingfpvalue.setText(Float.toString((AMLV + 1) * AMLV)); 

      float FP = NumberUtils.toFloat(pref.getString("fp", "")); 
      float HLTH = NumberUtils.toFloat(pref.getString("healthfpvalue", "")); 
      float CHR = NumberUtils.toFloat(charismafpvalue.getText().toString(), 0.0f); 
      float PER = NumberUtils.toFloat(persuasionfpvalue.getText().toString(), 0.0f); 
      float ELI = NumberUtils.toFloat(elicitationfpvalue.getText().toString(), 0.0f); 
      float AML = NumberUtils.toFloat(animalhandlingfpvalue.getText().toString(), 0.0f); 
      float MAN = NumberUtils.toFloat(pref.getString("manipulationfpvalue", "")); 
      float ACT = NumberUtils.toFloat(pref.getString("actingfpvalue", "")); 
      float SED = NumberUtils.toFloat(pref.getString("seductionfpvalue", "")); 
      float INTM = NumberUtils.toFloat(pref.getString("intimidationfpvalue", "")); 
      float INT = NumberUtils.toFloat(pref.getString("intelligencefpvalue", "")); 
      float TRP = NumberUtils.toFloat(pref.getString("trappingfpvalue", "")); 
      float FOR = NumberUtils.toFloat(pref.getString("forgingfpvalue", "")); 
      float CON = NumberUtils.toFloat(pref.getString("concoctionsfpvalue", "")); 
      float DEX = NumberUtils.toFloat(pref.getString("dexterityfpvalue", "")); 
      float ACR = NumberUtils.toFloat(pref.getString("acrobaticsfpvalue", "")); 
      float DOD = NumberUtils.toFloat(pref.getString("dodgingfpvalue", "")); 
      float RC = NumberUtils.toFloat(pref.getString("rangedfpvalue", "")); 
      float STR = NumberUtils.toFloat(pref.getString("strengthfpvalue", "")); 
      float MEL = NumberUtils.toFloat(pref.getString("meleefpvalue", "")); 
      float H2H = NumberUtils.toFloat(pref.getString("h2hfpvalue", "")); 
      float PRY = NumberUtils.toFloat(pref.getString("pryfpvalue", "")); 
      float WIT = NumberUtils.toFloat(pref.getString("witsfpvalue", "")); 
      float STL = NumberUtils.toFloat(pref.getString("stealthfpvalue", "")); 
      float WILD = NumberUtils.toFloat(pref.getString("wildernessfpvalue", "")); 
      float TRA = NumberUtils.toFloat(pref.getString("trackingfpvalue", "")); 
      float QI = NumberUtils.toFloat(pref.getString("qifpvalue", "")); 
      float FIRE = NumberUtils.toFloat(pref.getString("firefpvalue", "")); 
      float WAT = NumberUtils.toFloat(pref.getString("waterfpvalue", "")); 
      float EAR = NumberUtils.toFloat(pref.getString("earthfpvalue", "")); 
      float AIRV = NumberUtils.toFloat(pref.getString("airfpvalue", "")); 

      fpr.setText(Float.toString(FP - (QI + FIRE + WAT + EAR + AIRV + WIT + STL + WILD + TRA + STR + MEL + H2H + 
        PRY + MAN + ACT + SED + INTM + INT + TRP + FOR + CON + HLTH + CHR + PER + ELI + AML + DEX + ACR + DOD + RC))); 

      SharedPreferences.Editor editor=pref.edit(); 
      editor.putString("charisma", charisma.getText().toString()); 
      editor.putString("persuasion", persuasion.getText().toString()); 
      editor.putString("animalhandling", animalhandling.getText().toString()); 
      editor.putString("elicitation", elicitation.getText().toString()); 

      editor.putString("charismafpvalue", charismafpvalue.getText().toString()); 
      editor.putString("persuasionfpvalue", persuasionfpvalue.getText().toString()); 
      editor.putString("animalhandlingfpvalue", animalhandlingfpvalue.getText().toString()); 
      editor.putString("elicitationfpvalue", elicitationfpvalue.getText().toString()); 
      editor.commit(); 
     } 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

     } 
    }; 

    charisma.addTextChangedListener(watcher); 
    persuasion.addTextChangedListener(watcher); 
    elicitation.addTextChangedListener(watcher); 
    animalhandling.addTextChangedListener(watcher); 

} 

@Override 
public void onBackPressed() 
{ 
    Intent intent = new Intent (this, NewSkillsAndAttributes.class); 
     startActivity(intent); 
} 

}

的問題是這會導致應用程序崩潰一旦開始了活動。我哪裏錯了?

這裏的logcat的:

09-23 10:31:55.261: E/AndroidRuntime(1577): FATAL EXCEPTION: main 
09-23 10:31:55.261: E/AndroidRuntime(1577): Process: com.mikitz.rogsimple, PID: 1577 
09-23 10:31:55.261: E/AndroidRuntime(1577): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mikitz.rogsimple/com.mikitz.rogsimple.SaACharisma}: java.lang.NullPointerException 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at android.os.Handler.dispatchMessage(Handler.java:102) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at android.os.Looper.loop(Looper.java:136) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at java.lang.reflect.Method.invoke(Method.java:515) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at dalvik.system.NativeStart.main(Native Method) 
09-23 10:31:55.261: E/AndroidRuntime(1577): Caused by: java.lang.NullPointerException 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at com.mikitz.rogsimple.SaACharisma.onCreate(SaACharisma.java:180) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at android.app.Activity.performCreate(Activity.java:5231) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
09-23 10:31:55.261: E/AndroidRuntime(1577):  ... 11 more 
09-23 10:31:57.001: I/Process(1577): Sending signal. PID: 1577 SIG: 9 
+0

你確定他們已被正常初始化的意見(膽小和charismafpvalue)嗎? – Slampy 2014-09-23 14:47:10

+0

我很確定他們是,但以防萬一我已將整個'SaACharisma.class'添加到OP – Mikitz06 2014-09-23 14:51:30

+0

您的意思是「OP」? (英語不是我的母語) 你可以發表你的OnCreate方法 – Slampy 2014-09-23 14:54:57

回答

0

對於SharedPrefs我使用片斷。

public class SharedPrefsHelper 
{ 
    private static Map<String, Boolean> prefsBool = new ConcurrentHashMap<String, Boolean>(); 
    private static Map<String, String> prefsString = new ConcurrentHashMap<String, String>(); 
    private static Map<String, Long> prefsLong = new ConcurrentHashMap<String, Long>(); 

    public static SharedPreferences.Editor getEditor(Context ctx) 
    { 
     SharedPreferences sharedPreferences = ctx.getSharedPreferences(Constants.Prefs.PREFS_NAME, Context.MODE_PRIVATE); 
     return sharedPreferences.edit(); 
    } 

    public static SharedPreferences getPrefs(Context ctx) 
    { 
     return ctx.getSharedPreferences(Constants.Prefs.PREFS_NAME, Context.MODE_PRIVATE); 
    } 

    public static boolean getBoolean(Context context, String key, boolean defaultVal) 
    { 
     if (prefsBool.containsKey(key)) 
     { 
      return prefsBool.get(key); 
     } else 
     { 
      boolean val = getPrefs(context).getBoolean(key, defaultVal); 
      prefsBool.put(key, val); 
      return val; 
     } 
    } 

    public static String getString(Context context, String key, String defValue) 
    { 
     if (prefsString.containsKey(key)) 
     { 
      return prefsString.get(key); 
     } else 
     { 
      String val = getPrefs(context).getString(key, defValue); 
      prefsString.put(key, val); 
      return val; 
     } 
    } 
    public static long getLong(Context context, String key, long defValue) 
    { 
     if (prefsLong.containsKey(key)) 
     { 
      return prefsLong.get(key); 
     } else 
     { 
      long val = getPrefs(context).getLong(key, defValue); 
      prefsLong.put(key, val); 
      return val; 
     } 
    } 

    public static void putString(Context context, String key, String val) 
    { 
     prefsString.put(key, val); 
     SharedPreferences.Editor editor = getEditor(context); 
     editor.putString(key, val); 
     editor.commit(); 
    } 

    public static void putBoolean(Context context, String key, boolean val) 
    { 
     prefsBool.put(key, val); 
     SharedPreferences.Editor editor = getEditor(context); 
     editor.putBoolean(key, val); 
     editor.commit(); 
    } 
    public static void putLong(Context context, String key, long val) 
    { 
     prefsLong.put(key, val); 
     getEditor(context).putLong(key, val).commit(); 

    } 

} 
+0

我該如何去檢查'膽小' 'sanguine'在我的活動中有鑰匙? – Mikitz06 2014-09-23 15:46:43

+0

將類保存在Constans類中(使它們成爲靜態)...並使用任何你想要的 – Slampy 2014-09-24 08:09:15