Android : Android: Change a Button's text in a Dialog

on Saturday, September 20, 2014


I have in my Android App a Dialog which contains beside other things a Button. Now I want to change the text of that Button, but I get a NullPointer Exception.


The Button opens up a new Dialog (Datepicker) which gives the MainActivity a date (as a String) which I want then to use for the Button. Obviously I can declare the button and define him, but not interact. Within the try-statement (not sure whether it is a statement) the first line worked, but if I have the second line not commented and call it it crashes (or calls an exception, I added the try part later). I hope this is specific enough.


Here is the code from the button, the MainActivity and the Dialog the button lives in:


Dialog (.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();
}
}


Dialog (.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 -> Method that is causing the problem and method that calls the dialog (.java - partially)



public void openCreate(View view)
{
createDialogFragment = new CreateDialogFragment();
createDialogFragment.show(fragmentManager, "TEST");

updateSelectedButton(3);
}


This causes the problem:



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);
}
}


I hope this is specific enough and I did not forget any important code samples.


I appreciate any kind of productive help :)


John


0 comments:

Post a Comment