Android SharepReference Nullpointerexception?
我正在尝试为以下用户设置一些SharedReference:Android SharepReference Nullpointerexception?,android,sharedpreferences,Android,Sharedpreferences,我正在尝试为以下用户设置一些SharedReference: 名字 年龄 性别 重量 高度 活动水平 我已经实现了这段代码,但它给了我一个NullPointerException,这里的错误是什么 package com.nutriapp; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view
- 名字
- 年龄
- 性别
- 重量
- 高度
- 活动水平
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)
您没有在onCreate中执行将文本设置为高度的操作。。复制粘贴时要小心 您正在重新说明onCreate方法中的
levelofactivity
变量。它应该是会员
SharedPreferences customSharedPreference = getSharedPreferences("myCustomSharedPrefs", Activity.MODE_PRIVATE);
您的对象“CustomSharedReference”可以为空,但您将使用它,而不进行任何空检查
我建议你使用调试器来找出哪个对象是空的。
如果你告诉我们什么是空的,我们会更容易找到为什么我纠正了这个错误,但它仍然给了我同样的错误,我认为这个错误在SavePreferences methodeditor.putin(“levelpref”,levelofactivity.getCheckedRadioButtonId())的某个地方;我的错误实际上是我将levelofactivity radiogroup的id设置为radiogroup2,而它应该是radiogroup3;非常感谢:)