“无法在Android中检索到此错误的正确封闭实例”错误?

时间:2011-04-13 14:47:12

标签: java android

在下面给出的功能中,我尝试创建一个包含用户名和密码字段的alertDialog框,并将字段的值插入共享首选项。但< strong>在输入字段后按警告对话框中的确定按钮后,应用程序崩溃

我无法访问受保护的对话框onCreateDialog(int id)中alertDialog框内的正按钮单击侦听器内的变量。当我检查变量时 prefs,用户名和密码,它告诉我这个。 无法检索到正确的封闭实例。有人可以告诉我这是什么原因

       import android.app.Activity;
        import android.app.AlertDialog;
        import android.app.Dialog;
        import android.content.Context;
        import android.content.DialogInterface;
        import android.content.DialogInterface.OnClickListener;
        import android.content.SharedPreferences;
        import android.os.Bundle;
        import android.preference.PreferenceManager;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.widget.EditText;

        public class Login extends Activity {

            public static String PREF_LOGIN_USERNAME = "pref_username";
            public static String PREF_LOGIN_PASSWORD = "pref_password";

            public static final int DIALOG_LOGIN = 100;
            public static final int DIALOG_NEW_PASSWORD = 101;


            LayoutInflater factory;
            View loginView;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                showDialog(DIALOG_LOGIN);

            }

            protected Dialog onCreateDialog(int id) {
                switch (id) {

                case DIALOG_LOGIN:
                    // Inflating the View from the xml
                    factory = LayoutInflater.from(Login.this);
                    loginView = factory.inflate(R.layout.alert_dialog_text_entry, null);
                    return new AlertDialog.Builder(this)
                            .setTitle(R.string.alert_dialog_login)
                            .setView(loginView)
                            .setPositiveButton(R.string.dialog_ok,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int whichButton) {
                       Context context = getApplicationContext();
                            SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
                                    SharedPreferences.Editor editor = prefs.edit();

                                    EditText username = (EditText)loginView.findViewById(R.id.username_edit);
                                    EditText password = (EditText)loginView.findViewById(R.id.password_edit);
                                    editor.putString(PREF_LOGIN_USERNAME,
                                            username.getText().toString());
                                    editor.putString(PREF_LOGIN_PASSWORD,
                                            password.getText().toString());
                                    editor.commit();
                                    editor.commit();


                                        }
                                    })
                            .setNegativeButton(R.string.dialog_cancel,
                                    new OnClickListener() {

                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int which) {
                                            // TODO Auto-generated method stub
                                            showDialog(DIALOG_LOGIN);
                                        }
                                    }).create();
    }
    return null;
        }
    }

xml文件

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView 
        android:id="@+id/username_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="@string/alert_dialog_username"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/username_edit"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/password_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="@string/alert_dialog_password"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/password_edit"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

来自ddms的错误日志:

04-14 21:43:32.870: ERROR/AndroidRuntime(16125): java.lang.NullPointerException
04-14 21:43:32.870: 
ERROR/AndroidRuntime(16125):     at com.qrcoder.Login$1.onClick(Login.java:66)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at android.os.Looper.loop(Looper.java:123)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at java.lang.reflect.Method.invoke(Method.java:521)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at dalvik.system.NativeStart.main(Native Method)

修改:这是工作代码。

1 个答案:

答案 0 :(得分:1)

我不确定发生了什么,但我建议将来自showDialog的电话转移到onCreate。当UI实际出现在屏幕上时,最好在onResume中执行此操作。

编辑好的,我想我知道发生了什么。您正尝试在onCreate方法中为您的活动初始化字段用户名和密码,但我猜这些字段仅存在于登录对话框中。在构造对话框本身之后,您需要找到这些字段,因为它们在此之前不存在。