复选框堪称Android系统时器isChecked(),它给空指针异常 [英] when isChecked() of checkbox called in android, it gives null pointer exception

查看:217
本文介绍了复选框堪称Android系统时器isChecked(),它给空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的新的。我试图执行以下code,但应用程序崩溃,
在以下code,如果条件导致的NullPointerException这是粗体文字
请检查以下code ......
我分享我的code。感谢.....

 私人无效showGroupChatDialog()
    {
        ContentResolver的CR = getContentResolver();        Imps.ProviderSettings.QueryMap设置=新Imps.ProviderSettings.QueryMap(
                CR,mLastProviderId,假/ *不保持更新* /空/ *无程序* /);        字符串chatDomain =发布会。 + settings.getDomain();        settings.close();
     //这个例子说明了如何自定义布局添加到AlertDialog
        LayoutInflater厂= LayoutInflater.from(本);
        最后查看textEntryView = factory.inflate(R.layout.alert_dialog_group_chat,NULL);
        最终的TextView tvServer =(TextView中)textEntryView.findViewById(R.id.chat_server);        tvServer.setText(chatDomain);        新AlertDialog.Builder(本)
            .setTitle(R.string.create_or_join_group_chat)
            .setView(textEntryView)
            .setPositiveButton(R.string.connect,新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface对话,诠释whichButton){                    / *用户点击OK等做一些东西* /                    字符串聊天室= NULL;
                    字符串的ChatServer = NULL;
                    INT enablegrouphistory;
                    // INT opentoall;                    TextView的电视=(TextView中)textEntryView.findViewById(R.id.chat_room);                    聊天室= tv.getText()的toString()。                    电视=(TextView中)textEntryView.findViewById(R.id.chat_server);                    。的ChatServer = tv.getText()的toString();                    **复选框enablehistory =(复选框)findViewById(R.id.enable_group_history);
                    如果(enablehistory.isChecked()){
                        enablegrouphistory = 1;
                    }
                    复选框openall =(复选框)findViewById(R.id.open_to_all);
                    如果(openall.isChecked()){
                        opentoall = 1;
                    } **                    startGroupChat(聊天室,的ChatServer,((IMAPP)getApplication())的getConnection(mLastProviderId));                }
            })
            .setNegativeButton(R.string.cancel,新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface对话,诠释whichButton){                    / *用户点击取消,以便做一些东西* /
                }
            })
            .create()显示()。    }


解决方案

我猜的意见属于 alert_dialog_group_chat.xml

所以更改

 复选框enablehistory =(复选框)findViewById(R.id.enable_group_history);

 复选框enablehistory =(复选框)textEntryView.findViewById(R.id.enable_group_history);

同样

 复选框openall =(复选框)textEntryView.findViewById(R.id.open_to_all);

您也越来越 NullPointerException异常 findViewById 查找在当前膨胀布局的视图。

I am new in android. i am trying to execute following code but the application crashes, In following code, if condition causes the nullpointerexception which is in bold text please check the following code...... I am sharing my code. thanks.....

private void showGroupChatDialog ()
    {
        ContentResolver cr = getContentResolver();

        Imps.ProviderSettings.QueryMap settings = new Imps.ProviderSettings.QueryMap(
                cr, mLastProviderId, false /* don't keep updated */, null /* no handler */);

        String chatDomain = "conference." + settings.getDomain();

        settings.close();


     // This example shows how to add a custom layout to an AlertDialog
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.alert_dialog_group_chat, null);
        final TextView tvServer = (TextView) textEntryView.findViewById(R.id.chat_server);

        tvServer.setText(chatDomain);

        new AlertDialog.Builder(this)
            .setTitle(R.string.create_or_join_group_chat)
            .setView(textEntryView)
            .setPositiveButton(R.string.connect, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked OK so do some stuff */

                    String chatRoom = null;
                    String chatServer = null;
                    int enablegrouphistory;
                    //int opentoall;

                    TextView tv = (TextView)textEntryView.findViewById(R.id.chat_room);

                    chatRoom = tv.getText().toString();

                    tv = (TextView) textEntryView.findViewById(R.id.chat_server);

                    chatServer = tv.getText().toString();

                    **CheckBox enablehistory = (CheckBox) findViewById(R.id.enable_group_history);
                    if (enablehistory.isChecked()) {
                        enablegrouphistory = 1;
                    }
                    CheckBox openall = (CheckBox) findViewById(R.id.open_to_all);
                    if(openall.isChecked()){
                        opentoall = 1;
                    }**

                    startGroupChat (chatRoom, chatServer, ((ImApp)getApplication()).getConnection(mLastProviderId));

                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked cancel so do some stuff */
                }
            })
            .create().show();

    }

解决方案

I guess the views belongs to alert_dialog_group_chat.xml.

So change

CheckBox enablehistory = (CheckBox) findViewById(R.id.enable_group_history);

to

CheckBox enablehistory = (CheckBox) textEntryView.findViewById(R.id.enable_group_history);

Similarly

CheckBox openall = (CheckBox) textEntryView.findViewById(R.id.open_to_all);

You are getting NUllPointerException as findViewById looks for a view in the current inflated layout.

这篇关于复选框堪称Android系统时器isChecked(),它给空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆