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

如何解决Android中出现“无法检索到正确的封闭实例”错误?

| 在下面的给定功能中,我试图创建一个包含用户名和密码字段的alertDialog对话框,并将这些字段的值插入共享的首选项中。 我无法访问受保护的对话框onCreateDialog(int id)内的alertDialog框肯定按钮单击侦听器中的变量。当我检查变量时 首选项,用户名和密码向我显示了这一点。 无法检索此的正确封闭实例。有人可以告诉我原因是什么
       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)
编辑:这是工作代码。     

解决方法

我不确定发生了什么,但建议您将通话从moving4移到moving3ѭ。当UI实际上在屏幕上时,最好以ѭ5进行操作。 编辑好的,我想我知道发生了什么事。您正在尝试为您的活动初始化onCreate方法中的用户名和密码字段,但是我猜测这些字段仅存在于登录对话框中。在构造对话框本身之后,您需要查找这些字段,因为在此之前它们不存在。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res