-1

我正在尝试为以下用户设置一些共享首选项:

  • 姓名
  • 年龄
  • 性别
  • 重量
  • 高度
  • 活动水平

我已经实现了这段代码,但它给了我一个NullPointerException,这里的错误是什么?

  package com.nutriapp;

  import android.app.Activity;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.RadioButton;
  import android.widget.RadioGroup;

public class ProfilePreferenceActivity extends Activity {

RadioButton rb_male;
RadioButton rb_female;
RadioButton rb_light;
RadioButton rb_moderate;
RadioButton rb_heavy;
EditText name;
EditText age, weight, height;
RadioGroup genderradiogroup;
RadioGroup levelofactivity;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createprofile);
    SharedPreferences customSharedPreference = getSharedPreferences(
            "myCustomSharedPrefs", Activity.MODE_PRIVATE);

    name = (EditText) findViewById(R.id.namefield);
    name.setText(customSharedPreference.getString("namepref", ""));

    age = (EditText) findViewById(R.id.agefield);
//  int Age = Integer.parseInt(age.getText().toString());
    age.setText(customSharedPreference.getString("agepref", ""));

    genderradiogroup = (RadioGroup) findViewById(R.id.radioGroup2);
    rb_male = (RadioButton) findViewById(R.id.maleradiobutton);
    rb_female = (RadioButton) findViewById(R.id.femaleradiobutton);
    genderradiogroup.check(customSharedPreference.getInt("genderprefs",
            rb_male.getId()));

    weight = (EditText) findViewById(R.id.weightfield);
    //int Weight = Integer.parseInt(weight.getText().toString());
    weight.setText(customSharedPreference.getString("weightpref", ""));

    height = (EditText) findViewById(R.id.heightfield);
    //int Height = Integer.parseInt(height.getText().toString());
    weight.setText(customSharedPreference.getString("heightpref", ""));

    RadioGroup levelofactivity = (RadioGroup) findViewById(R.id.radioGroup2);
    rb_light = (RadioButton) findViewById(R.id.lightradiobutton);
    rb_moderate = (RadioButton) findViewById(R.id.moderateradiobutton);
    rb_heavy=(RadioButton) findViewById(R.id.heavyradiobutton);
    levelofactivity.check(customSharedPreference.getInt("levelpref", rb_light.getId()));


    Button addUser = (Button) findViewById(R.id.checkcreateprofilebutton);



    addUser.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            savePreferences();
            finish();

        }
    });

}

private void savePreferences(){

    SharedPreferences customSharedPreference = getSharedPreferences("myCustomSharedPrefs", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = customSharedPreference.edit();
    editor.putString("namepref", name.getText().toString());
    editor.putString("agepref", age.getText().toString());
    editor.putInt("genderprefs", genderradiogroup.getCheckedRadioButtonId());
    editor.putString("heightpref", height.getText().toString());
    editor.putString("weightpref", weight.getText().toString());
    editor.putInt("levelpref", levelofactivity.getCheckedRadioButtonId());
    editor.commit(); 
  }   
}

这是日志:

04-24 13:36:00.062: E/AndroidRuntime(797): FATAL EXCEPTION: main
04-24 13:36:00.062: E/AndroidRuntime(797): java.lang.NullPointerException
04-24 13:36:00.062: E/AndroidRuntime(797):  at com.nutriapp.ProfilePreferenceActivity.savePreferences(ProfilePreferenceActivity.java:86)
04-24 13:36:00.062: E/AndroidRuntime(797):  at com.nutriapp.ProfilePreferenceActivity.access$0(ProfilePreferenceActivity.java:77)
04-24 13:36:00.062: E/AndroidRuntime(797):  at com.nutriapp.ProfilePreferenceActivity$1.onClick(ProfilePreferenceActivity.java:69)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.view.View.performClick(View.java:3511)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.view.View$PerformClick.run(View.java:14105)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.os.Handler.handleCallback(Handler.java:605)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.os.Looper.loop(Looper.java:137)
04-24 13:36:00.062: E/AndroidRuntime(797):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 13:36:00.062: E/AndroidRuntime(797):  at java.lang.reflect.Method.invokeNative(Native Method)
04-24 13:36:00.062: E/AndroidRuntime(797):  at java.lang.reflect.Method.invoke(Method.java:511)
04-24 13:36:00.062: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 13:36:00.062: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 13:36:00.062: E/AndroidRuntime(797):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1
SharedPreferences customSharedPreference = getSharedPreferences("myCustomSharedPrefs", Activity.MODE_PRIVATE);

您的对象“customSharedPreference”可能为空,但您将使用它,无需任何空检查。

我建议你使用调试器来找出什么对象是空的。如果你告诉我们什么是空的,我们会更容易找出原因

于 2012-04-24T13:53:14.630 回答
0

您在 onCreate 中没有这样做setText..height复制粘贴时要小心..

您正在levelofactivityonCreate 方法中重新声明变量。应该是会员。。

于 2012-04-24T13:45:20.213 回答