Android:在对话框中查找按钮
我有一个对话框,它被我的MainActivity调用。在这个对话框里面有一个自定义按钮。当我试图让该按钮在我的MainActivity与Android:在对话框中查找按钮
Button createDateButton = (Button) findViewById(R.id.create_date_button);
是总是空(所以后来我得到一个空指针异常)。我认为是与对话框中的按钮有关...
该对话框扩展DialogFragment。它在运行时被称为MainActivity内部,作为对另一个Button的反应。如果您需要了解更多信息,请告诉我
下面是一些代码:
MainActivity(部分,因为它有200多个线)
public class MainActivity extends Activity implements UseDateInParentActivityInterface
{
FragmentManager fragmentManager = getFragmentManager();
CreateDialogFragment createDialogFragment;
DialogFragment datePicker;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homeFragment = new HomeFragment();
fragmentManager.beginTransaction().add(R.id.mainFrame, homeFragment).commit();
findViewById(R.id.bottomButton_home).setBackgroundResource(R.drawable.ic_home);
}
public void openCreate(View view)
{
createDialogFragment = new CreateDialogFragment();
createDialogFragment.show(fragmentManager, "TEST");
updateSelectedButton(3);
}
public void pickDate(View v)
{
datePicker = new DatePickerFragment();
datePicker.show(getFragmentManager(), String.format(getResources().getString(R.string.pickDate)));
}
public void dateForWhatever(int year, int month, int day)
{
DateFormatter dateFormatter = new DateFormatter();
String date = dateFormatter.toStringDE(year, month, day);
Button createDateButton = (Button) findViewById(R.id.create_date_button);
if (createDateButton != null)
{
createDateButton.setText(date);
}
else
{
System.out.println("bloeder Button ist gleich null");
}
}
}
这是DialogFragment的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.File
<?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"
>
<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>
约翰
在你CreateDialogFragment
,修改onCreateDialog()
:
@Override
public Dialog onCreateDialog(Bundle savedInstance)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View root = layoutInflater.inflate(R.layout.dialog_create, null);
builder.setView(root).setMessage(R.string.create);
// ...
Button createDateBtn = (Button) root.findViewById(R.id.create_date_button);
// set click listener here ...
return builder.create();
}
的问题是,该按钮在Dialog
,而不是Activity
的观层次。如果您从您的活动中调用findViewById()
,您将始终得到空:层次结构存在于两个不同的窗口中。
这段代码获取对话框中按钮的句柄。它从对话框的根视图(不是活动)中调用findViewById()
并检索按钮。然后,您可以将其保存到一个参考*以后使用,或直接在那里设置一个点击监听器。在这个监听器中,你可以编写自己的逻辑,例如调用一个你的活动的方法。
它的作品,令人惊叹!!!!!你真的帮我解决了为什么它不起作用。非常感谢!! – JRsz 2014-09-21 12:58:09
试试这个: 按钮createDateButton =(按钮)rootView.findViewById(R.id.create_date_button);
这应该是工作。
我更新了我的答案。 – 2014-09-20 20:44:49