context.getContextResolved appliaction stopped - begginner in java

Posted by Szymad on Stack Overflow See other posts from Stack Overflow or by Szymad
Published on 2012-10-28T16:54:28Z Indexed on 2012/10/28 17:00 UTC
Read the original article Hit count: 293

Filed under:
|

I have a problem with my app. I'm trying to execute query, but app stops every time. This error occurs while trying to execute query. I'm learing from Android Pro 3 book, but code presented in this book is deprecated.

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;
    }
}

FROM 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

I'm trying to learn android.

© Stack Overflow or respective owner

Related posts about android

Related posts about context