安卓:空指针异常对话框中 [英] Android: Null Pointer Exception in Dialog

查看:260
本文介绍了安卓:空指针异常对话框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在它显示有两个edittextboxes一个对话框。

I have a dialog box to be shown with two edittextboxes in it.

当我满山遍野并点击提交按钮,我需要保存在数据库中的值。我试图让在字段中输入的值,但空指针异常。

When I fill the fields and click on the submit button, I need to save the values in database. I am trying to get the values entered in the fields, but the Null Pointer Exception is raised.

我的code为如下

 View v = getLayoutInflater().inflate(R.layout.sample, null);
            new AlertDialog.Builder(Context)
                    .setTitle(R.string.Details)
                    .setView(v)
                    .setNegativeButton(R.string.Cancel,
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method stub

                                }
                            })
                    .setPositiveButton(R.string.Submit,
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    final EditText txtname=(EditText)findViewById(R.id.editText1);
                                    final EditText txtnumber=(EditText)findViewById(R.id.editText2);
                                    Save(txtname.getText().toString(),txtnumber.getText().toString());
                                }

当我调试和检查,它显示为空txtName的和txtnumber值。为什么我在哪里出了错?我使用的是布局,以显示领域

when I debug and checked, It shows the txtname and txtnumber values as empty. Where did I went wrong? I was using a layout to show the fields

布局,

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TableRow>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Name" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="195dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:ems="10"
        android:hint="FirstName LastName"
        android:inputType="textPersonName" />

</TableRow>

<TableRow>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Phone" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="190dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:ems="10"
        android:inputType="number"
        android:maxLength="10" />

</TableRow>

请帮我解决这个例外!

推荐答案

txtName的 txtnumber 为空

final EditText txtname=(EditText)findViewById(R.id.editText1);
 final EditText txtnumber=(EditText)findViewById(R.id.editText2);

当您试图访问空引用您将获得 NullPointerException异常

when you are trying to access null reference you will get NullPointerException

尽量让他们像这样

final EditText txtname=(EditText)v.findViewById(R.id.editText1);
 final EditText txtnumber=(EditText)v.findViewById(R.id.editText2);

这篇关于安卓:空指针异常对话框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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