내 Android 앱에 Button 옆에있는 대화 상자가 있습니다. 이제 해당 단추의 텍스트를 변경하고 싶지만 NullPointer 예외가 발생합니다.Android : 대화 상자에서 버튼의 텍스트 변경
이 버튼은 MainActivity에 (Button으로) 데이트를 사용하고자하는 날짜를 제공하는 새로운 Dialog (Datepicker)를 엽니 다. 분명히 버튼을 선언하고 정의 할 수는 있지만 상호 작용은 할 수 없습니다. try-statement 내에서 (문장이 있는지는 확실하지 않음) 첫 번째 행은 작동하지만 두 번째 행에 주석을 달지 않고 호출하면 충돌이 발생하거나 예외가 발생합니다. 나중에 try 부분을 추가했습니다. 나는 이것이 충분히 구체적이기를 바란다. 여기
버튼의 코드이다에서 MainActivity하고 대화 버튼 거주지 :대화 (된 .java)
public class CreateDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstance) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
builder.setView(layoutInflater.inflate(R.layout.dialog_create, null))
.setMessage(R.string.create)
.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return builder.create();
}
}
대화 (.XML)
<?xml version="1.0" encoding = "utf-8"?>
<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:id = "@+id/dialog_create"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background="@color/create_bg_1"
tools:context = ".CreateDialogFragment">
<EditText
android:id = "@+id/dialog_create_name"
android:layout_width = "match_parent"
android:layout_height = "@dimen/textFieldHight"
android:text = "@string/create_name"
android:background="@color/white"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp" />
<Button
android:layout_width = "wrap_content"
android:layout_height = "@dimen/textFieldHight"
android:ems = "6"
android:text = "@string/date"
android:background="@color/white"
android:id = "@+id/create_date_button"
android:onClick = "pickDate"
android:layout_below = "@id/dialog_create_name" />
</RelativeLayout>
MainActivity -> 문제를 일으키는 메서드 및 대화 상자를 호출하는 메서드 (.java - 부분적으로)
이문제의 원인 : 당신은 변경해야
public void dateForWhatever(int year, int month, int day) {
DateFormatter dateFormatter = new DateFormatter();
String date = dateFormatter.toStringDE(year, month, day);
try {
Button createDateButton = (Button) findViewById(R.id.create_date_button);
createDateButton.setText(date);
} catch (Exception cannotFindButton) {
System.out.println("Geh kacken\n" + year + " " + month + " " + day);
}
}
-1 당신의 대답은 연산 문제를 해결하는 방법에 대한 설명이 없습니다 때문입니다. 당신이 설명을 추가하면 나는 그것을 upvote 것입니다. –
나는 당신의 대답을 정말로 이해하지 못합니다. 그리고 positiveButton과 NegativeButton을 변경하지 않고 Button이 runtume에서 명확하게 chaanged ... – JRsz