我是android开发的新手。我擅长Java,但不擅长Android API。这是我的求职申请测试程序,所以我需要帮助来解决问题。我创建了对话框,通过按下按钮调用,但我无法访问对话框的textview(当我尝试访问它时应用程序崩溃 - 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("U\u017Eduotis s\u0117kmingai \u012Fgyvendinta");
}
private void btnApie() {
// TODO Auto-generated method stub
dlgApie.show();
}
private void btnForma() {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
在对话框布局中搜索(现在您正在活动布局中搜索):
TextView dlgText = (TextView) dlgApie.findViewById(R.id.dlgText);