2010-05-01 5 views
4

그래서 이전에 질문 한 질문과 관련이 있습니다. 지정된 레이아웃을 사용하여 경고를 표시하려고합니다. 내 레이아웃은 다음과 같습니다사용자 지정 레이아웃이 실패한 경고 대화 상자

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/layout_root" 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:padding="10dp"> 
    <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:textColor="#FFF" /> 
</LinearLayout> 

그리고 코드를 호출하고 경고 대화 상자가 표시하려면

catch되지 않은 처리기 : 스레드의 주요 출사를

Context mContext = getApplicationContext(); 

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
    // use a custom View defined in xml 
    View view = LayoutInflater.from(mContext).inflate(R.layout.sell_dialog,  (ViewGroup) findViewById(R.id.layout_root)); 
    builder.setView(view); 
    builder.setPositiveButton(android.R.string.ok, new OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

      // do whatever you want with the input 
     } 
    }); 
    AlertDialog alertDialog = builder.create(); 

    alertDialog.show(); 

내가 그것을 실행하면 내 말은 오류가 잡히지 않은 예외로 인해 android.view.WindowManager $ NadTokenException : 창을 추가 할 수 없습니다 - 응용 프로그램에 대한 토큰 null이 아닙니다.

ndroid 개발 사이트와 그것을 알아낼 수 없습니다. 나는 명백한 무엇인가를 놓치고 있다고 생각한다. 그러나 그 수정은 나에게 뛰어 오르는 것이 아니다. 이 경고 대화 상자를 표시하려면 어떻게합니까?

답변

9

getApplicationContext()을 사용하지 마십시오. 이 방법은 Context (예 : Activity)에서만 사용할 수 있습니다. AlertDialog.BuilderContext을 사용합니다.

Here is a sample projectView을 기반으로 한 AlertDialog을 보여주는 제 책 중 하나입니다.

+0

매력처럼 작용했습니다. 정말 고마워! – cmptrer

+0

사용자 정의보기를 사용하는 경고 대화 상자의 예제에 관심이 있습니다. 그러나 링크에는 그러한 예제가 없습니다. – Nemi

+0

@ 네미 : 아, 네 말이 맞아. 미안합니다. 이것을 사용해보십시오. http://github.com/commonsguy/cwac-colormixer – CommonsWare

관련 문제