以下代码适用于模拟器,但无法在Samsung Galaxy S III上运行。
final String[] projection = new String[]
{ ContactsContract.Profile.DISPLAY_NAME };
String name = null;
final Uri dataUri = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
final ContentResolver contentResolver = getContentResolver();
final Cursor c = contentResolver.query(dataUri, projection, null, null, null);
try
{
if (c.moveToFirst())
{
name = c.getString(c.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME));
}
}
finally
{
c.close();
}
System.out.println(name);
以下是例外:
12-03 20:57:15.751: E/AndroidRuntime(28172): FATAL EXCEPTION: main
12-03 20:57:15.751: E/AndroidRuntime(28172): java.lang.RuntimeException: Unable to start activity ComponentInfo{ht.smca.flashligh/ht.smca.flashligh.MainActivity}: java.lang.NullPointerException
12-03 20:57:15.751: E/AndroidRuntime(28172): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
12-03 20:57:15.751: E/AndroidRuntime(28172): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-03 20:57:15.751: E/AndroidRuntime(28172): at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-03 20:57:15.751: E/AndroidRuntime(28172): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-03 20:57:15.751: E/AndroidRuntime(28172): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 20:57:15.751: E/AndroidRuntime(28172): at android.os.Looper.loop(Looper.java:137)
12-03 20:57:15.751: E/AndroidRuntime(28172): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-03 20:57:15.751: E/AndroidRuntime(28172): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 20:57:15.751: E/AndroidRuntime(28172): at java.lang.reflect.Method.invoke(Method.java:511)
12-03 20:57:15.751: E/AndroidRuntime(28172): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-03 20:57:15.751: E/AndroidRuntime(28172): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-03 20:57:15.751: E/AndroidRuntime(28172): at dalvik.system.NativeStart.main(Native Method)
12-03 20:57:15.751: E/AndroidRuntime(28172): Caused by: java.lang.NullPointerException
12-03 20:57:15.751: E/AndroidRuntime(28172): at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
12-03 20:57:15.751: E/AndroidRuntime(28172): at ht.smca.flashligh.MainActivity.onCreate(MainActivity.java:68)
12-03 20:57:15.751: E/AndroidRuntime(28172): at android.app.Activity.performCreate(Activity.java:5206)
12-03 20:57:15.751: E/AndroidRuntime(28172): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
12-03 20:57:15.751: E/AndroidRuntime(28172): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
12-03 20:57:15.751: E/AndroidRuntime(28172): ... 11 more
有什么建议吗?我这样做是出于学习目的,即研讨会。
答案 0 :(得分:24)
这将帮助您获取存储在设备上的所有者名称:
Cursor c = getApplication().getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
c.moveToFirst();
textView.setText(c.getString(c.getColumnIndex("display_name")));
c.close();
确保在清单中添加此权限:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
答案 1 :(得分:5)
您有java空指针异常,因此name为null。将您的system.out.println()放入try中,您将不会遇到此错误。获得名字之后,我真的不知道 - Clad
不是我的帖子,但你的答案是: Get Android Device Name(对于Android设备模型,我的坏)
android.os.Build.MODEL;
以下是两种方法:
How can I get the google username on Android?
How can I get the first name (or full name) of the user of the phone?
答案 2 :(得分:3)
ContentResolver cr=getContentResolver();
Cursor curser = cr.query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
if(curser.getCount()>0){
c.moveToFirst();
String name=curser.getString(curser.getColumnIndex(
ContactsContract.Profile.DISPLAY_NAME));
Toast.makeText(MainActivity.this, "name"+name, Toast.LENGTH_SHORT).show();
}
c.close();
答案 3 :(得分:2)
此代码将为所有者提供完整信息
试试这段代码:
public class EmailFetcher{
static String getName(Context context) {
Cursor CR = null;
CR = getOwner(context);
String id = "", name = "";
while (CR.moveToNext())
{
name = CR.getString(CR.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
return name;
}
static String getEmailId(Context context) {
Cursor CR = null;
CR = getOwner(context);
String id = "", email = "";
while (CR.moveToNext()) {
id = CR.getString(CR.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
email = CR.getString(CR.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
return email;
}
static Cursor getOwner(Context context) {
String accountName = null;
Cursor emailCur = null;
AccountManager accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccountsByType("com.google");
if (accounts[0].name != null) {
accountName = accounts[0].name;
String where = ContactsContract.CommonDataKinds.Email.DATA + " = ?";
ArrayList<String> what = new ArrayList<String>();
what.add(accountName);
Log.v("Got account", "Account " + accountName);
for (int i = 1; i < accounts.length; i++) {
where += " or " + ContactsContract.CommonDataKinds.Email.DATA + " = ?";
what.add(accounts[i].name);
Log.v("Got account", "Account " + accounts[i].name);
}
String[] whatarr = (String[])what.toArray(new String[what.size()]);
ContentResolver cr = context.getContentResolver();
emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, where, whatarr, null);
if (id != null) {
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
while (pCur.moveToNext())
{
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.v("Got contacts", "phone" + phone);
}
pCur.close();
}
}
return emailCur;
}
}
答案 4 :(得分:0)
ContentResolver cr=getContentResolver();
Cursor curser = cr.query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
if(curser.getCount()>0){
c.moveToFirst();
String name=curser.getString(curser.getColumnIndex(
ContactsContract.Profile.DISPLAY_NAME));
Toast.makeText(MainActivity.this, "name"+name, Toast.LENGTH_SHORT).show();
}
c.close();
使用上面的代码从android设备获取所有者名称。基本上,这将为我的用户获取存储在联系人中的姓名。
注意:少数用户会遇到以下异常情况
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460) at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) at android.database.CursorWrapper.getString(CursorWrapper.java:137)
此异常不是特定于电话或特定于用户的。某些用户未在联系人列表中设置自己的联系人。因此,为解决此问题,启动应用程序的用户需要在“联系人”应用程序中设置第一行即ME联系人。只要在个人信息中输入您的姓名,就会生成卡。
@Leejjon ^^