为什么我不能更新我的视图片段phoneNumberEditText?

时间:2015-02-23 00:15:12

标签: android android-intent android-activity android-fragments android-fragmentactivity

我将电话号码作为字符串返回。我在我的logcat中验证了它。我正在尝试使用phoneNumberEditText.setText(phone);设置在班级private EditText phoneNumberEditText;中定义的电话号码我也尝试将电话转为CharSequence

我一直收到此错误

Caused by: java.lang.NullPointerException at com.mayday.md.common.ContactPickerFragment.onActivityResult(ContactPickerFragment.java:117)

这是班级。我在活动中捕获结果并将参数传递给onActivityResult。

package com.mayday.md.common;

import com.mayday.md.R;
import com.mayday.md.fragment.MessageTextFragment;

import android.app.FragmentManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;


import static android.app.Activity.RESULT_OK;
import static android.content.Intent.ACTION_GET_CONTENT;
import static android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE;
import static android.view.View.OnClickListener;

public class ContactPickerFragment extends Fragment {
    private static final int PICK_CONTACT_REQUEST_ID = 100;

    private ImageButton contactPickerButton;
    private EditText phoneNumberEditText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.contact_picker_fragment, container, false);

        contactPickerButton = (ImageButton) view.findViewById(R.id.contact_picker_button);
        phoneNumberEditText = (EditText) view.findViewById(R.id.contact_edit_text);


        return view;
    }

//    private OnClickListener contactPickerListener = new OnClickListener() {
//        @Override
//        public void onClick(View view) {
//            launchContactPicker(view);
//        }
//    };

//    @Override
//    public void onViewCreated(View view, Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//        contactPickerButton.setOnClickListener(contactPickerListener);
//    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        contactPickerButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 int wizardState = ApplicationSettings.getWizardState(getActivity());
//               if(wizardState != AppConstants.WIZARD_FLAG_HOME_READY){
                     AppConstants.IS_BACK_BUTTON_PRESSED = true;
                     AppConstants.IS_BACK_BUTTON_PRESSED = true;
//               }
                launchContactPicker(v);
            }
        });
    }

    public void launchContactPicker(View view) {
        Intent contactPickerIntent = new Intent(ACTION_GET_CONTENT);
        contactPickerIntent.setType(CONTENT_ITEM_TYPE);
        startActivityForResult(contactPickerIntent, PICK_CONTACT_REQUEST_ID);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data, Context context) {
        Log.e("ContactPickerFragment", "onActivityResult requestCode "+requestCode);
        Log.e("ContactPickerFragment", "onActivityResult resultCode "+resultCode);
        Log.e("ContactPickerFragment", "onActivityResult data "+data);

        ContentResolver cr = context.getContentResolver();
        Cursor cur = cr.query(data.getData(), null, null, null, null);
        String phone = "";
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
                Log.e("ContactPickerFragment", "onActivityResult id "+id);
                String name = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                Log.e("ContactPickerFragment", "onActivityResult name "+name);
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    //Query phone here.  Covered next
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                            new String[]{id}, null);
                    Log.e("WizardActivity", "onActivityResult pCur "+pCur);
                    while (pCur.moveToNext()) {
                        // Do something with phones
                                phone = pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Log.e("ContactPickerFragment", "onActivityResult phone "+phone);
                        }
                    pCur.close();
                }
            }
        }
        Log.e("ContactPickerFragment", "onActivityResult phone "+phone);

        //phoneNumberEditText.setText(phone);
        //phoneNumberEditText.setText((CharSequence)phone);
    }

}

这是logcat,我将实际的数字和名称

02-22 19:05:32.302  22501-22501/com.mayday.md E/WizardActivity﹕ onUserInteraction
02-22 19:05:32.392  22501-22501/com.mayday.md E/WizardActivity﹕ onUserInteraction
02-22 19:05:32.392  22501-22501/com.mayday.md E/WizardActivity.onPause﹕ page = setup-contacts
02-22 19:05:32.392  22501-22501/com.mayday.md E/>>>>>>﹕ assert flagRiseFromPause = true
02-22 19:05:32.422  22501-22501/com.mayday.md D/AbsListView﹕ unregisterIRListener() is called
02-22 19:05:33.022  22501-22501/com.mayday.md D/WizardActivity.onStop﹕ page = setup-contacts
02-22 19:05:33.022  22501-22501/com.mayday.md D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
02-22 19:05:33.022  22501-22501/com.mayday.md D/AbsListView﹕ unregisterIRListener() is called
02-22 19:05:37.512  22501-22501/com.mayday.md E/ContactPickerFragment﹕ onActivityResult requestCode 65636
02-22 19:05:37.512  22501-22501/com.mayday.md E/ContactPickerFragment﹕ onActivityResult resultCode -1
02-22 19:05:37.512  22501-22501/com.mayday.md E/ContactPickerFragment﹕ onActivityResult data Intent { dat=content://com.android.contacts/data/2369 flg=0x1 }
02-22 19:05:37.542  22501-22501/com.mayday.md E/ContactPickerFragment﹕ onActivityResult id 2369
02-22 19:05:37.542  22501-22501/com.mayday.md E/ContactPickerFragment﹕ onActivityResult name XXXX XXXXX
02-22 19:05:37.542  22501-22501/com.mayday.md E/WizardActivity﹕ onActivityResult pCur android.content.ContentResolver$CursorWrapperInner@42d55b78
02-22 19:05:37.542  22501-22501/com.mayday.md E/ContactPickerFragment﹕ onActivityResult phone xxxxxxxxxx
02-22 19:05:37.552  22501-22501/com.mayday.md E/ContactPickerFragment﹕ onActivityResult phone xxxxxxxxxx
02-22 19:05:37.552  22501-22501/com.mayday.md D/AndroidRuntime﹕ Shutting down VM
02-22 19:05:37.552  22501-22501/com.mayday.md W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4172fda0)
02-22 19:05:37.552  22501-22501/com.mayday.md E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mayday.md, PID: 22501
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65636, result=-1, data=Intent { dat=content://com.android.contacts/data/2369 flg=0x1 }} to activity {com.mayday.md/com.mayday.md.WizardActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3663)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3706)
            at android.app.ActivityThread.access$1400(ActivityThread.java:173)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5579)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.mayday.md.common.ContactPickerFragment.onActivityResult(ContactPickerFragment.java:117)
            at com.mayday.md.WizardActivity.onActivityResult(WizardActivity.java:334)
            at android.app.Activity.dispatchActivityResult(Activity.java:5643)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3659)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3706)
            at android.app.ActivityThread.access$1400(ActivityThread.java:173)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5579)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

就在你拥有的地方之前:

//phoneNumberEditText.setText(phone);
//phoneNumberEditText.setText((CharSequence)phone);

尝试添加以下两行,以检查您的控件引用或电话号码是否以某种方式变为空:

Log.e("phoneNumberEditText is null:" + phoneNumberEditText == null);
Log.e("phone string is null:" + phone == null);

查看其中任何一个是否输出为真。

答案 1 :(得分:0)

它看起来你的数据库中的手机是空的,你应该在设置到编辑文本字段之前进行验证/修剪手机。

phone = phone==null?"":phone.trim();
phoneNumberEditText.setText(phone);