Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

java - Get Owner Name of an Android Device

The following code works on an emulator, but fails to run on 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);

Here is the exception:

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

Any Suggestions? I do this for learning purposes, i.e. for a seminar.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This will help you get the owner name stored on the device:

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();

Make sure you add this permission in the manifest:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...