复选框确定运行哪个公式

时间:2014-09-23 14:36:14

标签: java android checkbox

我试图让某些复选框的检查确定要运行的公式。该类的代码如下。问题中的代码是注释掉的代码。

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

1 个答案:

答案 0 :(得分: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();

    }

}