phone 数字查询后 EditText 为空 - Android

EditText empty after phone number query - Android

我在一个名为 addContact() 的对话框中有一个按钮,单击该按钮后,它应该在我的 Android phone 中打开我的 Contacts,它确实如此,并且然后当我点击一个联系人时,它应该从该联系人中提取 phone 号码并将其放入我的对话框中的 EditText 中,但这不起作用,它显示空白 EditText 场地。由于 EditText 在片段 xml 中(不在我的 activity xml 中),我必须使用 LayoutInflator 并将其放入 View 之前实例化我的 EditText,所以我想知道这是否与我的错误有关。问题是,我没有收到任何崩溃或错误代码,只有在单击联系人后出现空白的 EditText。我已经用另一种方法膨胀了对话框视图(见下文),但它在一个方法内部,所以我担心范围,所以当我需要它时我在 onActivityResult() 中再次膨胀它。那是对的吗?如何显示 phone 号码?谢谢。

编辑:

如果我取出第二个inflation:

final LayoutInflater inflater = SMSMain.this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_compose_sms, null);
EditText phoneEntry = (EditText)dialogView.findViewById(R.id.sms_phone);

然后我在这一行得到一个空指针,当我使用第一个 inflation: 中的 EditText 变量时

etPhoneNumber.setText(phone);

但为什么会为空?

我的activity代码的一部分,SMSMain.java:

public void composeSMS(View v){

        // build your dialog box
        AlertDialog.Builder builder = new AlertDialog.Builder(SMSMain.this);
        // Get the layout inflater & inflate the box
        final LayoutInflater inflater = SMSMain.this.getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.dialog_compose_sms, null);
        builder.setView(dialogView);
        builder.setTitle("Send an SMS");
        builder.setPositiveButton("Send", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // get phone number and message IDs
                etPhoneNumber = (EditText)dialogView.findViewById(R.id.sms_phone);
                etMessage = (EditText) dialogView.findViewById(R.id.sms_message);
                // Turns the entered phone number and message into String
                String phoneNumber = etPhoneNumber.getText().toString().trim();
                String message = etMessage.getText().toString().trim(); 
                // error check to make sure there is input
                if (phoneNumber.length() > 0 && message.length() > 0) 
                    // sends the SMS
                    sendSMS(phoneNumber, message);                
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter a phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User cancelled the dialog
                dialog.cancel();
            }
        });
        // put all your builders in one create command
        AlertDialog dialog = builder.create(); 
        // show the dialog box
        dialog.show();

    }

    // method for sending the SMS
    private void sendSMS(String phoneNumber, String message)
        {        
            PendingIntent pi = PendingIntent.getActivity(this, 0,
                                    new Intent(this, SMSMain.class), 0);                
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, pi, null);        
        }

public void addContact(View v) {

        // add a contact. using Android's ContactPicker
        Intent i = new Intent(Intent.ACTION_PICK,
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
        startActivityForResult(i, CONTACT_PICKER_RESULT);

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case CONTACT_PICKER_RESULT:
                Cursor cursor = null;
                String phone = "";
                try{
                    Uri result = data.getData();
                    // get the contact id from the URI
                    String id = result.getLastPathSegment();

                    // query for phone number
                    cursor = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            null,
                            ContactsContract.CommonDataKinds.Phone._ID + "=?",
                            new String[]{id}, null);

                    int phoneIdx = cursor.getColumnIndex(Phone.DATA);

                    if (cursor.moveToFirst()) {
                        phone = cursor.getString(phoneIdx);
                    } else {
                        // Failed
                      }
                 } catch (Exception e) {
                        // Failed
                 } finally {
                        if (cursor != null){
                            cursor.close();
                        }
                        final LayoutInflater inflater = SMSMain.this.getLayoutInflater();
                        final View dialogView = inflater.inflate(R.layout.dialog_compose_sms, null);
                        EditText phoneEntry = (EditText)dialogView.findViewById(R.id.sms_phone);
                        phoneEntry.setText(phone);
                        if (phone.length() == 0) {
                            Toast.makeText(this, "No phone number found for contact.",
                                                                Toast.LENGTH_LONG).show();
                        }
                    }
                }
        }   

我的 dialog_compose_sms.xml(我的 activity.xml 里没什么东西,只有一个 "Compose SMS" 按钮。)

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp" >

        <Button
            android:id="@+id/contact_button"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:layout_marginLeft="2dp"
            android:text="+"
            android:onClick="addContact" />

        <EditText
            android:id="@+id/sms_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="phone" >

        <requestFocus />
        </EditText>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#e9e9e9"
        android:padding="20dp" >

    <Button
        android:id="@+id/speech_button"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="85dp"
        android:background="@drawable/speech_icon" />

    <Button
        android:id="@+id/mic_button"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginLeft="30dp"
        android:background="@drawable/mic_icon" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#e9e9e9"
        android:padding="20dp" >

    <EditText
        android:id="@+id/sms_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"

        android:inputType="textMultiLine" />

    </LinearLayout>

</LinearLayout>

onActivityResult 删除第二个 inflation 后

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case CONTACT_PICKER_RESULT:
                Cursor cursor = null;
                String phone = "";
                try{
                    Uri result = data.getData();
                    // get the contact id from the URI
                    String id = result.getLastPathSegment();

                    // query for phone number
                    cursor = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            null,
                            ContactsContract.CommonDataKinds.Phone._ID + "=?",
                            new String[]{id}, null);

                    int phoneIdx = cursor.getColumnIndex(Phone.DATA);

                    if (cursor.moveToFirst()) {
                        phone = cursor.getString(phoneIdx);
                        System.out.println("Value of phone: " + phone);
                    } else {
                        // Failed
                      }
                 } catch (Exception e) {
                        // Failed
                 } finally {

                     System.out.println("I am in finally block");

                     etPhoneNumber.setText(phone, TextView.BufferType.EDITABLE);
                     String stringNo = etPhoneNumber.getText().toString();
                     System.out.println("Value of etPhoneNumber: " + stringNo);


                    if (phone.length() == 0) {
                            Toast.makeText(this, "No phone number found for contact.",
                                                                Toast.LENGTH_LONG).show();
                    }
                     if (cursor != null){
                            cursor.close();
                     }

                 } // end finally
                 break;  
            }
        } 

Logcat删除第二个 inflation 后

12-31 21:07:02.430: E/AndroidRuntime(32266): FATAL EXCEPTION: main
12-31 21:07:02.430: E/AndroidRuntime(32266): Process: org.azurespot.practiceapp, PID: 32266
12-31 21:07:02.430: E/AndroidRuntime(32266): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/data/1343 flg=0x1 }} to activity {org.azurespot.practiceapp/org.azurespot.practiceapp.sms.SMSMain}: java.lang.NullPointerException
12-31 21:07:02.430: E/AndroidRuntime(32266):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3663)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3706)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at android.app.ActivityThread.access00(ActivityThread.java:173)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at android.os.Looper.loop(Looper.java:136)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at android.app.ActivityThread.main(ActivityThread.java:5579)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at java.lang.reflect.Method.invokeNative(Native Method)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at java.lang.reflect.Method.invoke(Method.java:515)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at dalvik.system.NativeStart.main(Native Method)
12-31 21:07:02.430: E/AndroidRuntime(32266): Caused by: java.lang.NullPointerException
12-31 21:07:02.430: E/AndroidRuntime(32266):    at org.azurespot.practiceapp.sms.SMSMain.onActivityResult(SMSMain.java:135)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at android.app.Activity.dispatchActivityResult(Activity.java:5643)
12-31 21:07:02.430: E/AndroidRuntime(32266):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3659)
12-31 21:07:02.430: E/AndroidRuntime(32266):    ... 11 more
12-31 21:07:05.510: I/Process(32266): Sending signal. PID: 32266 SIG: 9
12-31 21:07:05.860: I/PersonaManager(32648): getPersonaService() name persona_policy
12-31 21:07:05.860: D/skia(32648): GFXPNG PNG bitmap created width:72 height:72 bitmap id is 270 
12-31 21:07:05.870: D/skia(32648): GFXPNG PNG bitmap created width:144 height:144 bitmap id is 271 
12-31 21:07:05.880: D/skia(32648): GFXPNG PNG bitmap created width:48 height:48 bitmap id is 272 
12-31 21:07:05.890: E/MoreInfoHPW_ViewGroup(32648): Parent view is not a TextView
12-31 21:07:05.900: D/skia(32648): GFXPNG PNG bitmap created width:144 height:144 bitmap id is 273 
12-31 21:07:05.970: I/Adreno-EGL(32648): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
12-31 21:07:05.970: I/Adreno-EGL(32648): OpenGL ES Shader Compiler Version: E031.24.00.08+13
12-31 21:07:05.970: I/Adreno-EGL(32648): Build Date: 03/20/14 Thu
12-31 21:07:05.970: I/Adreno-EGL(32648): Local Branch: 0320_AU200_patches
12-31 21:07:05.970: I/Adreno-EGL(32648): Remote Branch: 
12-31 21:07:05.970: I/Adreno-EGL(32648): Local Patches: 
12-31 21:07:05.970: I/Adreno-EGL(32648): Reconstruct Branch: 
12-31 21:07:06.010: D/OpenGLRenderer(32648): Enabling debug mode 0

so I inflated it again when I needed it inside the onActivityResult(). Is that correct?

否因为您膨胀了当前未添加到 AlertDialog 中的新布局视图,向用户显示。

使用与您在 onActivityResult() 中的肯定按钮点击相同的实例 etPhoneNumber 向用户显示数字:

finally {
        if (cursor != null){
            cursor.close();
           }
          if(dialog.isShowing())
            etPhoneNumber.setText(phone);
           if (phone.length() == 0) {
               Toast.makeText(this, "No phone number found for contact.",
                                                     Toast.LENGTH_LONG).show();
           }
        }

还在 AlertDialog builder.setView 之后移动 etPhoneNumberetMessage 的初始化,因为目前您正在点击“发送”按钮时初始化两者。