I am popping up a dialog box where user enters the username and password. For testing purposes i want to give an alert message containing the username that the user entered in an EditText when he/she clicks Login. However my app crashes whenever login is pressed. Note: The app doesn't crash if i remove the EditText and just popup a toast message upon clicking Login. Here is my implementation:
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//get the layout inflater
final LinearLayout loginLayout = (LinearLayout) findViewById(R.id.popup_signin);
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.popup_signin, null))
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int d){
//sign in the user...
String username = ((EditText) loginLayout.findViewById(R.id.username)).getText().toString();
//String password = ((EditText) findViewById(R.id.password)).getText().toString();
Toast.makeText(getApplicationContext(), "Hello World "+username, Toast.LENGTH_LONG).show();
// new PostLogin(username,password).execute();
}
popup_signin.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_signin"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<EditText
android:id="@+id/username"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:typeface="monospace"
android:hint="enter your ID"/>
</LinearLayout>
LogCat
07-08 01:03:15.812: E/AndroidRuntime(28048): FATAL EXCEPTION: main
07-08 01:03:15.812: E/AndroidRuntime(28048): java.lang.NullPointerException
07-08 01:03:15.812: E/AndroidRuntime(28048): at com.example.hkuapp.MainActivity$PopUpDialog$2.onClick(MainActivity.java:61)
07-08 01:03:15.812: E/AndroidRuntime(28048): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
07-08 01:03:15.812: E/AndroidRuntime(28048): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 01:03:15.812: E/AndroidRuntime(28048): at android.os.Looper.loop(Looper.java:137)
07-08 01:03:15.812: E/AndroidRuntime(28048): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-08 01:03:15.812: E/AndroidRuntime(28048): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 01:03:15.812: E/AndroidRuntime(28048): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 01:03:15.812: E/AndroidRuntime(28048): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-08 01:03:15.812: E/AndroidRuntime(28048): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-08 01:03:15.812: E/AndroidRuntime(28048): at dalvik.system.NativeStart.main(Native Method)
Your EditText
is null because it is part of popup_signin
and you are getting it from loginLayout
.
So In your case you should Inflate your XML layout into a View object
, then find the editText
in the view
- then you can pass your view to the builder.ie Change
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.popup_signin, null))
to
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = (View) inflater.inflate(R.layout.popup_signin, null)
builder.setView(view)
and in Onclick
String username = ((EditText)loginLayout.findViewById(R.id.username)).getText().toString();
to
String username = ((EditText) view.findViewById(R.id.username)).getText().toString();
ie
Rewrite your code as
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//get the layout inflater
final LinearLayout loginLayout = (LinearLayout) findViewById(R.id.popup_signin);
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = (View) inflater.inflate(R.layout.popup_signin, null)
builder.setView(view))
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int d){
//sign in the user...
String username = ((EditText) view.findViewById(R.id.username)).getText().toString();
//String password = ((EditText) findViewById(R.id.password)).getText().toString();
Toast.makeText(getApplicationContext(), "Hello World "+username, Toast.LENGTH_LONG).show();
// new PostLogin(username,password).execute();
}
You're trying to find your EditText in loginLayout, that is null. Inflate your layout then assign it to loginLayout. Like:
LayoutInflater inflater = getActivity().getLayoutInflater();
View loginLayout = inflater.inflate(R.layout.popup_signin, null);
builder.setView(loginLayout);
...
I used a combination of the first answer and the second answer and it worked
LayoutInflater inflater = getActivity().getLayoutInflater();
View loginLayout = inflater.inflate(R.layout.popup_signin, null);
eFullName = (AppCompatEditText) loginLayout.findViewById(R.id.eName);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.