1

EditTexts の初期入力の設定に問題があります。以前のアクティビティからの文字列を含むインテントを渡すと、強制的に閉じられます。

私のプログラムの主な要点は、前のアクティビティが文字列を含むインテントを editText アクティビティに送信することです。初期化されていない場合、editTexts は空白になります。それ以外の場合は、前の画面の TextView に表示された値が含まれます。これが私のコードです:

EditText month, day, year;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lab2_082588birthday);

    Intent startUp = getIntent();
    String receivedString = startUp.getStringExtra(Lab2_082588part2.BIRTHDAY_STRING);

    if(receivedString.trim().length() > 0){
        String[] separated = receivedString.split("/");

        int stringMonth = Integer.parseInt(separated[0]);
        int stringDay = Integer.parseInt(separated[1]);
        int stringYear = Integer.parseInt(separated[2]);

        month.setText(stringMonth);
        day.setText(stringDay);
        year.setText(stringDay);

    }
}

これが私のLogCatです

07-06 15:05:19.918: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException

07-06 15:05:19.918: E/AndroidRuntime(276):  at com.android.rondrich.Lab2_082588birthday.onCreate(Lab2_082588birthday.java:34)

07-06 15:05:19.918: E/AndroidRuntime(276):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

07-06 15:05:19.918: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
4

3 に答える 3

1

receivedStringが 2 つのスラッシュ ("/") を持つようにフォーマットされていない場合、文字列配列separatedには必要な 3 つの値が含まれません。

これにより、IndexOutOfBoundsException.

編集:

スラッシュ文字はバックスラッシュでエスケープする必要があります"\/"

于 2012-07-06T07:08:58.357 に答える
0

まず、textView を次のように初期化 setContentView(R.layout.lab2_082588birthday);します。

month = (TextView)findViewById(R.id.month);
 day = (TextView)findViewById(R.id.day);
 month = (TextView)findViewById(R.id.year);

次に、Integer値を次のように設定しTextViewます。

month.setText(stringMonth+"");
day.setText(stringDay+"");
year.setText(stringDay+"");

また

month.setText(String.valueOf(stringMonth));
day.setText(String.valueOf(stringDay));
year.setText(String.valueOf(stringDay));

logcat を提供した後: String を int にペアリングする前に配列の長さを確認します。

if(separated.length >0)
{
        int stringMonth = Integer.parseInt(separated[0]);
        int stringDay = Integer.parseInt(separated[1]);
        int stringYear = Integer.parseInt(separated[2]);

        month.setText(stringMonth+"");
        day.setText(stringDay+"");
        year.setText(stringDay+"");
}
于 2012-07-06T06:58:30.623 に答える
0

この行を変更する必要があります

    month.setText(stringMonth);
    day.setText(stringDay);
    year.setText(stringDay);

by bcz stringMonth 、stringDay などは整数であり、テキストビューでこのように設定する必要があります

    month.setText(""+stringMonth);
    day.setText(""+stringDay);
    year.setText(""+stringDay);

他の方法は

    month.setText(String.valueOf(stringMonth));
    day.setText(String.valueOf(stringDay));
    year.setText(String.valueOf(stringDay));
于 2012-07-06T07:08:43.453 に答える