安卓:无法访问对话框文本视图



我是android开发的新手。我擅长Java,但不擅长Android API。这是我的工作申请测试程序,所以我需要帮助来解决这个问题。我制作了对话框,它是通过按下按钮来调用的,但我无法访问对话框的文本视图(当我试图访问它时应用程序崩溃-NullPointerException),即使Eclipse IDE也没有发现任何问题。我包含了几乎所有的代码,因为我不知道要排除

custom_dialog.xml

<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/dlgText"
              android:layout_width="fill_parent"
              android:layout_height="50dp"
              android:textColor="#FFF"
              />
</LinearLayout>

BalticAmadeusActivity.java

public class BalticAmadeusActivity extends Activity{
    /** Called when the activity is first created. */
    Button btnApie;
    Button btnForma;
    Dialog dlgApie;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnApie = (Button) findViewById(R.id.btnApie);
        btnForma = (Button) findViewById(R.id.btnForma);
        btnApie.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (v.equals(btnForma)) {
                    btnForma();
                } else if (v.equals(btnApie)) {
                    btnApie();
                }
                return false;
            }
        });
        dlgApie = new Dialog(this.getApplicationContext(), android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
        dlgApie.setContentView(R.layout.custom_dialog);
        TextView dlgText = (TextView) findViewById(R.id.dlgText);  
        dlgText.setText("Gera uzduotis"); //at this line, app crashes
        //dlgText.setText("Uu017Eduotis su0117kmingai u012Fgyvendinta");
    }
    private void btnApie() {
        // TODO Auto-generated method stub
        dlgApie.show();
    }
    private void btnForma() {
        // TODO Auto-generated method stub
    }
}

在对话框布局中搜索(现在您正在活动布局中搜索):

TextView dlgText = (TextView) dlgApie.findViewById(R.id.dlgText); 

最新更新