Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android findViewById in DialogFragment

Tags:

java

android

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 DialogFragment that handles the date picker.

I tried getView but the application crashes at the specific point.

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$1.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

like image 952
Mike Rodios Avatar asked Apr 13 '13 17:04

Mike Rodios


People also ask

What is findViewById () method used for?

FindViewById(Int32)Finds a view that was identified by the android:id XML attribute that was processed in #onCreate .

Is DialogFragment deprecated?

Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.

What is r in findViewById in Android?

R is a Class that contains the ID's of all the Views. findViewById is the method that finds the View by the ID it is given. So findViewById(R. id. myName) finds the View with name 'myName'.

How do I destroy DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its dialog.


2 Answers

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

@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 :

@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(); } 
like image 157
hardartcore Avatar answered Sep 21 '22 18:09

hardartcore


There is more simple way to find views in DialogFragment.

@Override public void onStart() {     super.onStart();     mTextView = (TextView) getDialog().findViewById(R.id.tvDate); } 
like image 41
Igor Konyukhov Avatar answered Sep 23 '22 18:09

Igor Konyukhov