Я пытаюсь получить значение EditText с помощью кода ниже. Действие используется для добавления двух строк. В макете я уже ставлю onSave и метод onCancel. Но когда я нажимаю кнопку, связанную с onSave, она показывает ошибку исключения нулевого указателя.
Активность:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class AddTimeActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.time_item);
}
public void onCancel(View view){
this.setResult(RESULT_CANCELED);
finish();
}
public void onSave(View view){
Intent intent = getIntent();
EditText timeView = (EditText)view.findViewById(R.id.time_view);
String time = timeView.getText().toString();
intent.putExtra(TimeTrackerActivity.TIME_KEY, time);
EditText notesView = (EditText)view.findViewById(R.id.notes_view);
String notes = notesView.getText().toString();
intent.putExtra(TimeTrackerActivity.NOTES_KEY, notes);
this.setResult(RESULT_OK, intent);
finish();
}
}
Макет:
...
<EditText
android:id="@+id/timeView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>
<EditText
android:id="@+id/notes_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:gravity="top"
android:layout_weight="1"
android:layout_marginBottom="10dp"/>
...
<Button
android:onClick="onSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:onClick="onCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
...
Сообщение об ошибке отладчика
E/AndroidRuntime(1123): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1123): at com.timetracker.AddTimeActivity.onSave(AddTimeActivity.java:34)
Я пытался проверить и значение timeView через отладчик, и оно нулевое.
Может ли кто-нибудь помочь?