我正在尝试通过用户的活动获取联系号码(已选中)。如何解决这个问题?强制关闭而不是输出! 我已经阅读了很多教程,但这对我没那么有用!
package com.example.c_p;
import java.util.Iterator;
import java.util.Set;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int CONTACT_PICKER_RESULT = 1001;
public static final String DEBUG_TAG = null;
EditText emailEntry = (EditText) findViewById(R.id.invite_email);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doLaunchContactPicker(View view)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
switch (requestCode)
{
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String phone = "";
try {
Bundle extras = data.getExtras();
Set<String> keys = extras.keySet();
Iterator<String> iterate = keys.iterator();
while (iterate.hasNext()) {
String key = iterate.next();
Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]");
}
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
null);
int PhoneIdx = cursor.getColumnIndex(Phone.DATA);
if (cursor.moveToFirst()) {
phone = cursor.getString(PhoneIdx);
Log.v(DEBUG_TAG, "Got number: " + phone);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to Number", e);
}
finally {
if (cursor != null) {
cursor.close();
}
emailEntry.setText(phone);
if (phone.length() == 0) {
Toast.makeText(this, "No number found for contact.",
Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
}
答案 0 :(得分:2)
你有这个
EditText emailEntry = (EditText) findViewById(R.id.invite_email);
在onCreate
之外。您将获得NullPointeException
更改为
private EditText emailEntry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailEntry = (EditText) findViewById(R.id.invite_email);
}
findViewById
在当前夸大的布局中查找id为id的视图。您需要先将内容设置为活动,然后在onCreate
答案 1 :(得分:1)
这一行会给你一个错误
EditText emailEntry = (EditText) findViewById(R.id.invite_email);
在设置内容之前,您无法尝试初始化View
。在setContentView()
之后将其初始化为
EditText emailEntry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// not until here
emailEntry = (EditText) findViewById(R.id.invite_email);
由于您的视图存在于您的布局中,因此在使用inflater或setContentView()夸大布局之前,您无法像findViewById()那样找到它们。