我弹出一个对话框,用户输入用户名和密码。出于测试目的,我想给出一条警告消息,其中包含用户在单击“登录”时在EditText中输入的用户名。但是,只要按下登录,我的应用就会崩溃。 注意:如果我删除EditText并在单击Login时弹出Toast消息,应用程序不会崩溃。 这是我的实施:
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)
答案 0 :(得分:2)
您的EditText
为空,因为它是popup_signin
的一部分而您是从loginLayout
获取的。
因此,在您的情况下,您应该将XML布局扩展为View object
,然后在editText
中找到view
- 然后您可以将视图传递给builder.i.e。变化
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.popup_signin, null))
到
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = (View) inflater.inflate(R.layout.popup_signin, null)
builder.setView(view)
和Onclick
String username = ((EditText)loginLayout.findViewById(R.id.username)).getText().toString();
到
String username = ((EditText) view.findViewById(R.id.username)).getText().toString();
即。
将您的代码重写为
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();
}
答案 1 :(得分:0)
您正试图在loginLayout中找到您的EditText,即为null。膨胀您的布局然后将其分配给loginLayout。像:
LayoutInflater inflater = getActivity().getLayoutInflater();
View loginLayout = inflater.inflate(R.layout.popup_signin, null);
builder.setView(loginLayout);
...
答案 2 :(得分:0)
我使用了第一个答案和第二个答案的组合,它起作用了
LayoutInflater inflater = getActivity().getLayoutInflater();
View loginLayout = inflater.inflate(R.layout.popup_signin, null);
eFullName = (AppCompatEditText) loginLayout.findViewById(R.id.eName);