2011-10-28 30 views
-1

我想在Android中實現自定義對話框。但是,當我自己嘗試時,我無法運行我的代碼。無法在Android中實現自定義對話框的代碼

我得到一個錯誤在這些語句在Eclipse:
1)Button button1main = (Button) findViewById(R.id.*btn1*);

2)TextView text = (TextView) dialog.findViewById(R.id.*txt3*);

我的Android代碼:

package com.example; 

import android.app.Activity; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class Main2 extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.front); 

     Button button1main = (Button) findViewById(R.id.btn1); 
     button1main.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Dialog dialog = new Dialog(Main2.this); 
       dialog.setContentView(R.layout.maindialog); 
       dialog.setTitle("This is my custom dialog box"); 
       dialog.setCancelable(true); 

       TextView text = (TextView) dialog.findViewById(R.id.txt3); 
       text.setText(R.string.lots_of_text); 

       Button button = (Button) dialog.findViewById(R.id.Button01); 
       button.setOnClickListener(new OnClickListener() { 

        public void onClick(View v) { 
         finish(); 
        } 
       }); 

       dialog.show(); 
      } 

     }); 
    } 
} 

這裏是我的xml的: 前.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 


    <TextView 
    android:id="@+id/txt3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="This is my main activity, from here, I want to display a dialog, after the user clicked the button below this text."/> 

    <Button 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/txt3" 
     android:layout_width="wrap_content" 
     android:id="@+id/btn1" 
     android:text="Hey! There is more..."/> 

</RelativeLayout> 

maindialog.xml

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout> 

    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"> 

    <ScrollView> 
    android:id="@+id/ScrollView01" 
    android:layout_width="wrap_content" 
    android:layout_height="200px"> 

    <TextView> 
     android:text="@+id/txt3" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"> 
    </TextView> 

    </ScrollView> 


    <Button> 
    android:id="@+id/Button01" 
    android:layout_below="@id/ScrollView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="Cancel" 
    </Button> 

</RelativeLayout> 

我不能,直到我刪除上述statements..Can有人把紅線錯誤,幫我跑我在Eclipse代碼?

感謝

+0

你可以請詳細說明了什麼錯誤顯示? –

+0

這些陳述,我已經提到..在日食的錯誤是「btn1不能解決或不是一個領域」..類似txt3 –

+0

它是編譯時的錯誤 –

回答

3

您的XML語法錯誤。

<RelativeLayout> <- this is wrong 
    android:id="@+id/RelativeLayout01&quot; 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

您需要刪除「>」,這適用於所有元素,不僅相對佈局

<RelativeLayout 
    android:id="@+id/RelativeLayout01&quot; 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
+0

我是否還必須在我的xml文件中爲

+0

@ Parth_90是的.. –

+0

是的。屬性應該在所有元素的標籤內。 – Vladimir

相關問題