0

嗨,我正在尝试从联系人那里获取电话号码以发送彩信。我没有收到联系人,但我无法在EditText视图中设置该值。但我可以使用 Toast 显示该值。我收到以下异常。

06-25 22:29:39.101: E/AndroidRuntime(22406): FATAL EXCEPTION: main
06-25 22:29:39.101: E/AndroidRuntime(22406): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r423-2D45452D.2292i6960c86a891de3df/626 flg=0x1 (has extras) }} to activity {com.RajusLabs.IAmHere/com.RajusLabs.IAmHere.MMS}: java.lang.NullPointerException
06-25 22:29:39.101: E/AndroidRuntime(22406):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at android.os.Looper.loop(Looper.java:130)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at java.lang.reflect.Method.invokeNative(Native Method)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at java.lang.reflect.Method.invoke(Method.java:507)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at dalvik.system.NativeStart.main(Native Method)
06-25 22:29:39.101: E/AndroidRuntime(22406): Caused by: java.lang.NullPointerException
06-25 22:29:39.101: E/AndroidRuntime(22406):    at com.RajusLabs.IAmHere.MMS.onActivityResult(MMS.java:123)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-25 22:29:39.101: E/AndroidRuntime(22406):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-25 22:29:39.101: E/AndroidRuntime(22406):    ... 11 more

MMS.java源代码

public class MMS extends Activity {
    private static final int CAMERA_REQUEST = 500;
    private ImageView imageView;
    Intent i = null;
    Bitmap photo;
    EditText toNo,toMsg;
    String MMSNo,MMSmsg;
    ImageButton contacts;
    Uri bmpUri = null;
    final int PICK_CONTACT = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.mms);
  //full screen

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);


    this.imageView = (ImageView) findViewById(R.id.imgLoad);
    Button bt = (Button) findViewById(R.id.btCam);
    Button picok = (Button) findViewById(R.id.bPicOk);
    ImageButton contacts = (ImageButton)findViewById(R.id.btContacts);
    final EditText toNo = (EditText)findViewById(R.id.etMMSNo);
    final EditText toMsg = (EditText) findViewById(R.id.etMMSMsg);

    bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            startActivityForResult(intent, CAMERA_REQUEST);  
        }
    });

    picok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(photo != null)
            {
                String pathofBmp = Images.Media.insertImage(getContentResolver(), photo,"title", null);
                bmpUri = Uri.parse(pathofBmp);
            }
            MMSNo = toNo.getText().toString();
            MMSmsg = toMsg.getText().toString();

            if(bmpUri != null && MMSNo != null)
            {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.putExtra("address",MMSNo);
                i.putExtra("sms_body",MMSmsg);
                i.putExtra(Intent.EXTRA_STREAM,bmpUri);
                i.setType("image/*");
                startActivity(i);
            }
        }
    });
    contacts.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            toNo.setText("");
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
            startActivityForResult(intent, PICK_CONTACT);


        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

         switch(requestCode) {
          case (PICK_CONTACT) : {
                Uri contactData = data.getData();
                Cursor c = managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                    String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                    if (hasPhone.equalsIgnoreCase("1")) {
                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = " + id, null, null);
                        phones.moveToFirst();
                        String cNumber = phones.getString(phones.getColumnIndex("data1"));
                        String name=phones.getString(phones.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                        System.out.println("NAME:"+name+"\n NUMBER:"+cNumber);
                        Toast.makeText(getApplicationContext(),"Selected Contact Name:"+name+" and no: "+cNumber, Toast.LENGTH_LONG).show();
                        toNo.setText(cNumber);
                        }
                    else
                    {
                        Toast.makeText(getApplicationContext(),"Mobile Number not found.\nPlease enter Mobile Number by Manual", Toast.LENGTH_LONG).show();
                    }

            break;
          }
       }
          case (CAMERA_REQUEST): {
                photo = (Bitmap) data.getExtras().get("data");
                imageView.setImageBitmap(photo);
            }
        }

}
}

行号中的代码:toNo.setText(cNumber);

4

2 回答 2

3

消除

final EditText

final EditText toNo = (EditText)findViewById(R.id.etMMSNo);

它应该只是:

toNo = (EditText)findViewById(R.id.etMMSNo);

因为您已经将其声明为类的成员变量。放在final EditText它前面 inonCreate()使得它的作用域只对 有效onCreate(),所以它在 中为空onActivityResult。如果那有意义的话!

顺便说一句,我也会这样做,toMsg否则如果你尝试在外面使用它,你会遇到同样的问题onCreate()

在此处阅读有关变量范围的更多信息。

于 2013-06-25T17:11:54.103 回答
0

I would suggest you to:

-If one of the "EditText toNo" you declared is not necessary remove it from the code.

-The compilation error raised by the Edit Text is because when you declare a final variable, it will be immutable so you can't set a new value to it.

Try removing:

final EditText toNo and initialize the toNo you created outside of onCreate

于 2013-06-25T17:29:19.887 回答