2017-12-05 10 views
-1

아래 코드와 함께 오류가 무엇인지 말해 줄 수 있습니까? 날짜 선택기 대화 상자를 엽니 텍스트 편집을 사용하려고하지만 해결할 수없는 오류를 보여주는 것 방법 findViewById를 (INT)메서드를 확인할 수 없습니다. findViewById (int) - 날짜 선택기로 텍스트 편집

package com.example.claire.townapp; 
import java.util.Calendar; 
import android.annotation.SuppressLint; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.DialogFragment; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.DatePicker; 
import android.widget.EditText; 

@SuppressLint("ValidFragment") 
public class DateDialog extends DialogFragment implements 
DatePickerDialog.OnDateSetListener { 
EditText txtdate; 
public DateDialog(View view){ 
    txtdate=(EditText)view; 
} 
public Dialog onCreateDialog(Bundle savedInstanceState) { 


// Use the current date as the default date in the dialog 
    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) { 
    //show to the selected date in the text box 
    String date=day+"-"+(month+1)+"-"+year; 
    txtdate.setText(date); 
} 

public void onStart(){ 
    super.onStart(); 


    EditText txtDate = (EditText)findViewById(R.id.txtdate); 
    txtDate.setOnFocusChangeListener(new View.OnFocusChangeListener(){ 
     public void onFocusChange(View view, boolean hasfocus){ 
      if(hasfocus){ 
       DateDialog dialog=new DateDialog(view); 
       FragmentTransaction ft =getFragmentManager().beginTransaction(); 
       dialog.show(ft, "DatePicker"); 





    } 
} 

사람이 내가이 오류를 해결하는 방법을 알고 있나요? 어떤 도움을 많이 주시면 감사하겠습니다!

+1

조각에서 getView(). findViewById – elmorabea

답변

-1

죄송합니다. 이전에 시도한 내용을 이해하지 못했습니다. 편집 텍스트에 포커스가있을 때 대화 상자를 열려고합니다 (나는 조언하지 않습니다 - 더 나은 옵션은 onclick을 사용하지만 문제는 heres입니다 :). onstart에 쓴 모든 내용은 대화 상자 클래스에 있지 않아야하지만 편집 텍스트를 포함하는 부분 또는 활동에 있어서는 안됩니다. (내 오래된 대답을보고 관련 활동이나 단편에서 onStart에 넣지 말고 대신 onCreate에 넣으십시오).

이전 대답 : findviewbyid를 사용하려면 rootview가 필요합니다. 를 사용하여 XML 레이아웃 팽창하고 (그것이 당신의 주요 활동에서 수행하는 방식과 유사) 당신의 rootview를 얻고,이 같은 루트 뷰에서 fintviewbyid 전화보다 : 글고 txtDate로 = (글고) rootview .findViewById (R.id.txtdate);

하지만 뷰가 아직 작성되지 않았으므로 onStart 내부에서 수행 할 수 없습니다. 당신은 onCreateDialog 안에 그것을해야합니다.

+0

죄송합니다. 안드로이드에 비교적 익숙하지 않습니다. xml 레이아웃에 inflate를 어떻게 사용합니까? – user3481511

+0

LayoutInflater inflater = getActivity(). getLayoutInflater(); 뷰 rootView = inflater.inflate (R.layout.XML_NAME, null); –

0

코드가 작동하지 않는 이유는 findViewById (int resID)가 Fragment의 메소드가 아니고 Activity와 유사하기 때문입니다. findViewById()으로 전화를 걸려면보기와 같은 유효한 개체가 필요합니다. AlertDialogs는 View와 비슷한 View의 레이아웃을 확인할 수있게 해주는 유틸리티 메소드를 가지고 있습니다 (사실, ViewEvent 객체의 'mWindow'를 호출하기 만하면됩니다). 대화 상자에 대한 참조를 얻고 findViewById()을 호출하여 원하는 EditText에 대한 참조를 전달하십시오.

단편으로 DatePickerDialog를 래핑하기 때문에 레이아웃을 제어 할 수 없기 때문에 코드가 작동한다고 생각하지 않습니다. View의 창 내부를 알아야하는 프레임 워크 클래스를 사용하고 있으므로주의하십시오. 예를 들어. 나는 DataPickerDialog의 소스 코드를 엿보고 'txtdate'또는 심지어 EditText라는 ID를 가진 뷰에 대한 참조를 찾지 못했다. (내가 틀렸을 수도 있고 철저하게 보지 않았을 수도있다.) findViewById (int resid)에 해당 값을 전달하면 null이 반환됩니다. 해당 ID가 있지만 다른보기 유형의보기가 포함 된 경우 ClassCastException을 얻을 수도 있으므로보기 유형에 특별한주의를 기울여야합니다.