问题
This is an experiment for understanding what ContactsContract can offer. I don't know what I've done wrong... Please help me with this problem. My code:
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);
}
}
These error logs appears when I launch the application application:
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 ComponentInfo{com.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>
回答1:
Instantiate your TextView
inside onCreate
method.
Change TextView tv = (TextView) findViewById(R.id.textView);
to just TextView tv;
And add the following line in your onCreate
method after setContentView(R.layout.activity_main);
line:
tv = (TextView) findViewById(R.id.textView);
来源:https://stackoverflow.com/questions/34810318/android-open-the-devices-contact-list-with-an-intent-not-working-unable-to-in