安卓。打开设备的联系人列表,但 Intent 不起作用。无法实例化活动 ComponentInfo

Posted

技术标签:

【中文标题】安卓。打开设备的联系人列表,但 Intent 不起作用。无法实例化活动 ComponentInfo【英文标题】:Android. Open the device's Contact list with an Intent not working. Unable to instantiate activity ComponentInfo 【发布时间】:2016-04-21 00:03:50 【问题描述】:

这是一个了解 ContactsContract 可以提供什么的实验。我不知道我做错了什么......请帮我解决这个问题。 我的代码:

package com.example.beni.contactbrowser1;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity 
    static final int PICK_CONTACT_REQUEST = 1;
    TextView tv = (TextView) findViewById(R.id.textView);

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Button button= (Button) findViewById(R.id.b1);
        button.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                //DoIt(v);
                //alarm.setAlarm(MainActivity.this);
                pickContact();
            
        );
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) 
            return true;
        

        return super.onOptionsItemSelected(item);
    

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent )
        super.onActivityResult( requestCode, resultCode, intent );
        if ( requestCode == PICK_CONTACT_REQUEST ) 

            if ( resultCode == RESULT_OK ) 
                Uri pickedPhoneNumber = intent.getData();
                // handle the picked phone number in here.
                tv.setText(pickedPhoneNumber.toString());
            
        
    

    private void pickContact() 
        Intent pickContactIntent = new Intent( Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI );
        pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
    

这些错误日志在我启动应用程序时出现:

01-15 12:59:09.004 7447-7447/com.example.beni.contactbrowser1 D/AndroidRuntime: Shutting down VM
01-15 12:59:09.004 7447-7447/com.example.beni.contactbrowser1 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4001e6a8)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime: FATAL EXCEPTION: main
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfocom.example.beni.contactbrowser1/com.example.beni.contactbrowser1.MainActivity: java.lang.NullPointerException
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1760)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.ActivityThread.access$1500(ActivityThread.java:156)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1000)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:130)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.os.Looper.loop(SourceFile:351)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:3827)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:538)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:  Caused by: java.lang.NullPointerException
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.Activity.findViewById(Activity.java:1683)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at com.example.beni.contactbrowser1.MainActivity.<init>(MainActivity.java:19)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at java.lang.Class.newInstanceImpl(Native Method)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at java.lang.Class.newInstance(Class.java:1440)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.Instrumentation.newActivity(Instrumentation.java:1056)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1650)
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1760) 
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.ActivityThread.access$1500(ActivityThread.java:156) 
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1000) 
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:130) 
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.os.Looper.loop(SourceFile:351) 
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:3827) 
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:538) 
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969) 
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727) 
01-15 12:59:09.007 7447-7447/com.example.beni.contactbrowser1 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
01-15 12:59:09.009 258-263/? D/Settings/Provide: lookupValue, table secure cache.containsKey dropbox:data_app_crash
01-15 12:59:09.011 258-263/? D/Settings/Provide: lookupValue, table secure cache.containsKey send_action_app_error
01-15 12:59:09.011 258-263/? W/ActivityManager:   Force finishing activity com.example.beni.contactbrowser1/.MainActivity

AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.beni.contactbrowser1" >
    <uses-permission android:name="android.permission.READ_PROFILE"></uses-permission>
    <uses-permission android:name="android.permission.GET_TASKS"></uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

tv = (TextView) findViewById(R.id.textView); 移动到onCreate() 方法内部,位于setContentView() 之后。不过,将声明留在原处。 谢谢!我现在看到了... :) 【参考方案1】:

onCreate 方法中实例化您的TextView

TextView tv = (TextView) findViewById(R.id.textView); 更改为 TextView tv;

并在onCreate 方法中的setContentView(R.layout.activity_main); 行之后添加以下行:

tv = (TextView) findViewById(R.id.textView);

【讨论】:

以上是关于安卓。打开设备的联系人列表,但 Intent 不起作用。无法实例化活动 ComponentInfo的主要内容,如果未能解决你的问题,请参考以下文章

如何将方向设置为intent.putExtra?

安卓权威编程指南-笔记(第27章 broadcast intent)

苹果手机上的联系人怎么可以传到安卓手机上

如何使用 Intent 打开显示“组”选项卡的联系人?

SyncAdapter 用于检测设备本地联系人列表的变化

使用 Intent 从 WebView 拨打电话不起作用