2013-04-13 21 views
12

Próbuję utworzyć datepicker, który ustawia wartość widoku tekstowego po ustawieniu.Android findViewById w DialogFragment

Ale nie mogę wybrać telewizora z DialogFragment, który obsługuje datepicker.

Próbowałem getView(), ale aplikacja zawiesza się w określonym punkcie.

Oto mój kod:

import java.util.Calendar; 

import android.annotation.SuppressLint; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.DialogFragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.DatePicker; 
import android.widget.TextView; 

@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(" ")); 
} 

}

Dzięki, każda pomoc będzie mile widziane.

Aktualizacja z nowym kodem i następującego błędu:

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

+0

Czy "tvDate" znajduje się w innym fragmencie? – Sam

+0

tvDate jest wewnątrz działania –

+1

Prosimy o sprawdzenie mojej odpowiedzi na ten temat. http://stackoverflow.com/questions/16507016/findviewbyid-in-dialogfragment-nullpointerexception/29291399#29291399 Cheers! – noahutz

Odpowiedz

21

Aby uzyskać konkretny widok Fragment/Dialog fragmentem, który trzeba zastosować onCreateView(). Oto przykład jak to zrobić:

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

Sprawdź najpierw czy onCreateDialog() jest wywoływana przed onCreateView() a jeśli to prawda, spróbuj coś takiego:

@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(); 
} 
+0

Próbowałem tego, ale teraz aplikacja ulega awarii, gdy zaczyna ładować datepicker. –

+0

zaktualizuj swoją odpowiedź w sposób, w jaki go używasz, a awaria: – hardartcore

+0

ok, zaktualizowana kodem i błędem. –

1

Jest prostszy sposób na znalezienie poglądów w DialogFragment.

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