如何获取给定电话号码的联系人姓名

时间:2014-04-25 05:09:19

标签: android android-contacts contactscontract

这个我的代码我希望获得给定联系号码的联系人姓名但是应用程序被强行关闭。我得到的Logcat错误是Logcat我不知道哪里弄错了。请帮助我如何获取给定的Conact号码的联系人姓名。

package com.example.example2;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.Toast;

public class NextActvity extends Activity {
    String name;
    Context context = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        Toast.makeText(getApplicationContext(),
                getContactNameFromNumber("9013714449"), 1000).show();

    }

    private String getContactNameFromNumber(String number) {
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(number));

        Cursor cursor = context.getContentResolver().query(uri,
                new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
        if (cursor.moveToFirst()) {
            name = cursor.getString(cursor
                    .getColumnIndex(PhoneLookup.DISPLAY_NAME));
        }

        return name;
        // proceed as you need

    }

}

我的logcat

04-25 10:38:09.475: E/AndroidRuntime(25142): FATAL EXCEPTION: main
04-25 10:38:09.475: E/AndroidRuntime(25142): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.example2/com.example.example2.NextActvity}: java.lang.NullPointerException
04-25 10:38:09.475: E/AndroidRuntime(25142):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at android.app.ActivityThread.access$600(ActivityThread.java:165)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at android.os.Handler.dispatchMessage(Handler.java:107)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at android.os.Looper.loop(Looper.java:194)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at android.app.ActivityThread.main(ActivityThread.java:5391)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at java.lang.reflect.Method.invokeNative(Native Method)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at java.lang.reflect.Method.invoke(Method.java:525)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at dalvik.system.NativeStart.main(Native Method)
04-25 10:38:09.475: E/AndroidRuntime(25142): Caused by: java.lang.NullPointerException
04-25 10:38:09.475: E/AndroidRuntime(25142):    at com.example.example2.NextActvity.getContactNameFromNumber(NextActvity.java:35)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at com.example.example2.NextActvity.onCreate(NextActvity.java:20)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at android.app.Activity.performCreate(Activity.java:5122)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
04-25 10:38:09.475: E/AndroidRuntime(25142):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
04-25 10:38:09.475: E/AndroidRuntime(25142):    ... 11 more

2 个答案:

答案 0 :(得分:2)

在使用前分配您的上下文变量,如此

context = getApplicationContext();

答案 1 :(得分:1)

因为你的上下文是null。您可以使用NextActivity.this

等活动来代替上下文