-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」はnullになる可能性がありますが、nullチェックなしで操作できます。

デバッガーを使用して、どのオブジェクトが null であるかを調べることをお勧めします。何が null かを教えていただければ、その理由を突き止めるのがはるかに簡単になります。

于 2012-04-24T13:53:14.630 に答える
0

onCreateでやっているのではないsetTextheight..コピペに気をつけて..

levelofactivityonCreate メソッド内で変数を再宣言しています。メンバーになればいいのに..

于 2012-04-24T13:45:20.213 に答える