我无法从EditText获取文本。这个EditText位于一个名为optional的costum对话框中。我认为这与视图有关,但仍然无法弄清楚。
感谢您的帮助。
主要活动
public class MainActivity extends Activity {
Button button = (Button) findViewById(R.id.comments);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setTitle("Comentários");
dialog.setContentView(R.layout.optional);
final EditText editTextName = (EditText) arg0.findViewById(R.id.editTextName);
final EditText editTextEmail = (EditText) arg0.findViewById(R.id.editTextEmail);
final EditText editTextCompany = (EditText) arg0.findViewById(R.id.editTextCompany);
Button cancel = (Button) dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button submit = (Button) dialog.findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Null pointer exception
name = editTextName.getText().toString();
email = editTextEmail.getText().toString();
company = editTextCompany.getText().toString();
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
optional.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background"
tools:context="com.example.isqsurvey.MainActivity" >
<EditText
android:id="@+id/editTextName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textViewName"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editTextEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textViewEmail"
android:ems="10"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editTextCompany"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textViewCompany"
android:ems="10" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/submit"
android:layout_alignBottom="@+id/submit"
android:layout_alignParentLeft="true"
android:text="@string/cancel" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/submit" />
答案 0 :(得分:1)
将所有初始化更改为
final EditText editTextName = (EditText) dialog.findViewById(R.id.editTextName);
final EditText editTextEmail = (EditText) dialog.findViewById(R.id.editTextEmail);
final EditText editTextCompany = (EditText) dialog.findViewById(R.id.editTextCompany);
这是因为视图属于对话框布局optional.xml