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



这是我的代码,我想为给定的联系人号码获取联系人名称,但应用程序被强制关闭。我得到下面的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
    }
}

我的日志

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

在使用之前分配上下文变量,如

context = getApplicationContext();

这是因为您的上下文为null。您可以使用类似NextActivity.this 的活动来代替上下文

相关内容

  • 没有找到相关文章

最新更新