java DialogFragment 中的 Android findViewById

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15990995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:34:28  来源:igfitidea点击:

Android findViewById in DialogFragment

javaandroid

提问by Mike Rodios

I am trying to develop a date picker that sets the value of a text view when set.

我正在尝试开发一个日期选择器,在设置时设置文本视图的值。

But I cannot select the TV from the DialogFragmentthat handles the date picker.

但是我无法从DialogFragment处理日期选择器的电视中选择电视。

I tried getViewbut the application crashes at the specific point.

我试过了,getView但应用程序在特定点崩溃。

Here's my code:

这是我的代码:

@SuppressLint("NewApi")
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    TextView mTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

                // R.layout.my_layout - that's the layout where your textview is placed
        View view = inflater.inflate(R.layout.activity_register_screen, container, false);
        mTextView = (TextView) view.findViewById(R.id.tvDate);
        // you can use your textview.
        return view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        int syear = year;
        int smonth = month;
        int sday = day;
        // set selected date into textview
        mTextView.setText(new StringBuilder().append(smonth + 1)
           .append("-").append(sday).append("-").append(syear)
           .append(" "));
    }
}

Updated with new code and the following error:

更新了新代码和以下错误:

04-15 09:45:30.880: E/AndroidRuntime(23016): FATAL EXCEPTION: main
04-15 09:45:30.880: E/AndroidRuntime(23016): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
04-15 09:45:30.880: E/AndroidRuntime(23016):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:239)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at com.android.internal.app.AlertController.installContent(AlertController.java:236)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.AlertDialog.onCreate(AlertDialog.java:336)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.Dialog.dispatchOnCreate(Dialog.java:353)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.Dialog.show(Dialog.java:257)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.DialogFragment.onStart(DialogFragment.java:490)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.Fragment.performStart(Fragment.java:1537)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.BackStackRecord.run(BackStackRecord.java:622)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1386)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.FragmentManagerImpl.run(FragmentManager.java:430)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.os.Handler.handleCallback(Handler.java:605)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.os.Looper.loop(Looper.java:137)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at android.app.ActivityThread.main(ActivityThread.java:4517)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at java.lang.reflect.Method.invokeNative(Native Method)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at java.lang.reflect.Method.invoke(Method.java:511)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
04-15 09:45:30.880: E/AndroidRuntime(23016):    at dalvik.system.NativeStart.main(Native Method)

SOLVED

解决了

回答by h4rd4r7c0r3

To get a specific view in Fragment / Fragment dialog you should use onCreateView(). Here is an example how to do that :

要在 Fragment / Fragment 对话框中获得特定视图,您应该使用onCreateView(). 这是一个如何做到这一点的示例:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // R.layout.my_layout - that's the layout where your textview is placed
    View view = inflater.inflate(R.layout.my_layout, container, false);
    TextView mTextView = (TextView) view.findViewById(R.id.colors);
    // you can use your textview.
    return view;
}

Check first if onCreateDialog()is called before onCreateView()and if that's true try using something like this :

首先检查是否onCreateDialog()在之前被调用onCreateView(),如果这是真的尝试使用这样的东西:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout. activity_register_screen, null);
    builder.setView(view);
    // Create the AlertDialog object and return it
    return builder.create();
}

回答by Игорь Конюхов

There is more simple way to find views in DialogFragment.

在 DialogFragment 中有更简单的方法来查找视图。

@Override
public void onStart() {
    super.onStart();
    mTextView = (TextView) getDialog().findViewById(R.id.tvDate);
}