0
我無法從EditText獲取文本。該EditText位於名爲可選的Costum對話框中。我認爲這與觀點有關,但仍然無法解決。Edittext空指針異常
謝謝你的幫助。
主要活動
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" />
是的,你是對的。謝謝:) – JorgeSilva
@JorgeSilva歡迎您。您可以通過點擊旁邊的勾號來接受答案 – Raghunandan