将值设置为“EditText”时获取致命异常

时间:2013-06-25 17:09:36

标签: android android-activity android-edittext

您好我正在尝试从联系人那里获取电话号码以发送MMS。我正在联系不,但我无法在 EditText View中设置该值。但我可以使用联系人显示该值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);

2 个答案:

答案 0 :(得分:3)

删除

final EditText

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

应该是:

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

因为你已经将它声明为你班级的成员变量。在final EditText中将onCreate()置于其前面使其范围仅对onCreate()有效,因此在onActivityResult中为空。如果这有意义的话!

顺便说一句,我也会为toMsg执行此操作,否则如果您尝试在onCreate()之外使用它,则会遇到同样的问题。

详细了解变量范围here

答案 1 :(得分:0)

我建议你:

- 如果您声明的“EditText toNo”之一没有必要从代码中删除它。

- 编辑文本引发的编译错误是因为当您声明一个最终变量时,它将是不可变的,因此您无法为其设置新值。

尝试删除:

最终EditText toNo 并初始化你在onCreate

之外创建的toNo