2011-10-31 130 views
0

好吧,只需要知道,我複製粘貼來自android網站的代碼,所以我不認爲有什麼問題。對話框崩潰的應用程序

問題是,當我按下按鈕時,它崩潰了。它從對話框代碼崩潰,因爲我沒有任何其他的東西。

的代碼是:

MainDialog.xml:

  <?xml version="1.0" encoding="utf-8"?> 
      <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" 
      > 
      <ImageView 
      android:id="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_marginRight="10dp" 
      /> 
      <TextView android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:textColor="#FFF" 
      /> 

      </LinearLayout> 

而上的情況是BUTTON1按下:

    Context mContext = getApplicationContext(); 
     Dialog dialog = new Dialog(mContext); 

     dialog.setContentView(R.layout.maindialog); 
     dialog.setTitle("Custom Dialog"); 

     TextView text = (TextView) dialog.findViewById(R.id.text); 
     text.setText("Hello, this is a custom dialog!"); 
     ImageView image = (ImageView) dialog.findViewById(R.id.image); 
     image.setImageResource(R.drawable.icon);  
     dialog.show(); 
+0

你有回答嗎? –

回答

1

您使用Activity背景和添加一個調用dialog.show();

Context mContext = this; //Assumes you are calling this from within an activity 
    Dialog dialog = new Dialog(mContext); 

    dialog.setContentView(R.layout.maindialog); 
    dialog.setTitle("Custom Dialog"); 

    TextView text = (TextView) dialog.findViewById(R.id.text); 
    text.setText("Hello, this is a custom dialog!"); 
    ImageView image = (ImageView) dialog.findViewById(R.id.image); 
    image.setImageResource(R.drawable.icon);  
    dialog.show(); 
+0

好吧現在它在那裏。但它崩潰!我會把日誌放在第一篇文章中。 – user1015311

+0

您需要使用'Activity'上下文而不是'getApplicationContext()'。如果你正在一個活動中運行這一切,你可以調用'對話框對話框=新對話框(這個);' – plowman

+0

像一個魅力工作很多! – user1015311