2014-01-31 2 views
1

Utills 클래스 메서드를 호출하여 사용자 정의 Toast 메시지를 화면에 표시 할 수있게하려고합니다.Utills 클래스의 사용자 토스트

내 코드 : 나는

(ViewGroup) findViewById(R.id.custom_toast_layout) 

난 상태 컴파일 오류가 전화 라인 4와 proble이

static public void ShowToast(Context context,String message, int duration) 
{ 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    // Inflate the Layout 
    View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.custom_toast_layout)); 

    TextView text = (TextView) layout.findViewById(R.id.textToShow); 
    // Set the Text to show in TextView 
    text.setText(message); 

    Toast toast = new Toast(context); 
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); 
    toast.setDuration(duration); 
    toast.setView(layout); 
    toast.show(); 
} 

:

방법 findViewById를을 (INT)은 SystemUtills 유형에 대해 정의되지 않았습니다.

어떻게 해결할 수 있습니까?

감사합니다.

+0

참고 http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView%20. –

답변

3

findViewById는 활동의 방법, 그래서이 줄

View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.custom_toast_layout)); 

에 당신은 컴파일시 오류에 대한 이유 context.findViewById(R.id.custom_toast_layout)

이 필요합니다.

+0

나는 그것을 시도 : 그것을 시도 : "findViewById (int) 유형 컨텍스트에 대한 정의되지 않은"아이디어를 whay? –

+1

findViewById는 Context가 아니라 Activity 메서드입니다. Activity에서 ShowToast를 호출하고 "this"를 인수로 전달하는 경우 활동에 컨텍스트를 캐스팅 할 수 있습니다. (Activity) 컨텍스트 .findViewById – Blackbelt

+0

Ok, param 시작 및 해당 작업에 대한 (ViewGroup) 캐스트 추가. –