アプリに問題があります。クエリを実行しようとしていますが、毎回アプリが停止します。このエラーは、クエリを実行しようとしているときに発生します。私は Android Pro 3 の本から学んでいますが、この本に記載されているコードは非推奨です:
package com.example.contactsabuout;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.app.Activity;
import android.database.Cursor;
import android.util.Log;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private static Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainActivity.context = getApplicationContext();
Log.v("INFO", "Completed: onCreate.");
}
public static Context getAppContext() {
return MainActivity.context;
}
public void doQuery(View view) {
Uri peopleBaseUri = ContactsContract.Contacts.CONTENT_URI;
Log.v("II","Button clicked.");
Log.v("II", "Uri for ContactsContract.Contacts: " + peopleBaseUri);
Context context = getAppContext();
Log.v("II", "Got context: " + context);
Cursor cur;
Log.v("II", "Created cursor: cur");
cur = context.getContentResolver().query(peopleBaseUri, null, null, null, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
LogCat から
10-28 17:45:02.513: V/INFO(4677): Completed: onCreate.
10-28 17:45:02.613: D/libEGL(4677): loaded /system/lib/egl/libGLES_android.so
10-28 17:45:02.653: D/libEGL(4677): loaded /system/lib/egl/libEGL_adreno200.so
10-28 17:45:02.723: D/libEGL(4677): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
10-28 17:45:02.723: D/libEGL(4677): loaded /system/lib/egl/libGLESv2_adreno200.so
10-28 17:45:03.014: I/Adreno200-EGLSUB(4677): <ConfigWindowMatch:2078>: Format RGBA_8888.
10-28 17:45:03.054: D/OpenGLRenderer(4677): Enabling debug mode 0
10-28 17:45:03.254: D/OpenGLRenderer(4677): has fontRender patch
10-28 17:45:03.274: D/OpenGLRenderer(4677): has fontRender patch
10-28 17:45:12.873: V/II(4677): Button clicked.
10-28 17:45:12.873: V/II(4677): Uri for ContactsContract.Contacts: content://com.android.contacts/contacts, rest will be null
10-28 17:45:12.873: V/II(4677): Got context: android.app.Application@40d83d90
10-28 17:45:12.873: V/II(4677): Created cursor: cur
10-28 17:45:12.933: D/AndroidRuntime(4677): Shutting down VM
10-28 17:45:12.933: W/dalvikvm(4677): threadid=1: thread exiting with uncaught exception (group=0x40aaf228)
10-28 17:45:12.953: E/AndroidRuntime(4677): FATAL EXCEPTION: main
10-28 17:45:12.953: E/AndroidRuntime(4677): java.lang.IllegalStateException: Could not execute method of the activity
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.view.View$1.onClick(View.java:3071)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.view.View.performClick(View.java:3538)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.view.View$PerformClick.run(View.java:14330)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.os.Handler.handleCallback(Handler.java:608)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.os.Handler.dispatchMessage(Handler.java:92)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.os.Looper.loop(Looper.java:156)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.app.ActivityThread.main(ActivityThread.java:4977)
10-28 17:45:12.953: E/AndroidRuntime(4677): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 17:45:12.953: E/AndroidRuntime(4677): at java.lang.reflect.Method.invoke(Method.java:511)
10-28 17:45:12.953: E/AndroidRuntime(4677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-28 17:45:12.953: E/AndroidRuntime(4677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-28 17:45:12.953: E/AndroidRuntime(4677): at dalvik.system.NativeStart.main(Native Method)
10-28 17:45:12.953: E/AndroidRuntime(4677): Caused by: java.lang.reflect.InvocationTargetException
10-28 17:45:12.953: E/AndroidRuntime(4677): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 17:45:12.953: E/AndroidRuntime(4677): at java.lang.reflect.Method.invoke(Method.java:511)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.view.View$1.onClick(View.java:3066)
10-28 17:45:12.953: E/AndroidRuntime(4677): ... 11 more
10-28 17:45:12.953: E/AndroidRuntime(4677): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/contacts from pid=4677, uid=10155 requires android.permission.READ_CONTACTS
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.os.Parcel.readException(Parcel.java:1332)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.content.ContentProviderProxy.query(ContentProviderNative.java:406)
10-28 17:45:12.953: E/AndroidRuntime(4677): at android.content.ContentResolver.query(ContentResolver.java:315)
10-28 17:45:12.953: E/AndroidRuntime(4677): at com.example.contactsabuout.MainActivity.doQuery(MainActivity.java:47)
10-28 17:45:12.953: E/AndroidRuntime(4677): ... 14 more
私はアンドロイドを学ぼうとしています。