2016-09-25 111 views
0

我正在测试Developer site中的Listview示例,这是用Loader和Adapter填充ListView的示例。该应用程序因某些未知错误而失败。我不确定,但怀疑getLoaderManager().initLoader(0, null, this);声明有问题。如何用Loader填充ListView?

模拟器显示“不幸的是,ListViewLoader已停止。” 我也用过,<uses-permission android:name="android.permission.READ_CONTACTS" />之前<application>标签但收到相同的错误。在清单文件中。在logcat中显示以下信息:提前


09-25 22:57:33.853 28524-28618/com.example.listviewloader E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 
Process: com.example.listviewloader, PID: 28524 
java.lang.RuntimeException: An error occurred while executing doInBackground() 
at android.os.AsyncTask$3.done(AsyncTask.java:309) 
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) 
at java.util.concurrent.FutureTask.setException(FutureTask.java:223) 
at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{3c98c11 28524:com.example.listviewloader/u0a108} (pid=28524, uid=10108) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS 
at android.os.Parcel.readException(Parcel.java:1599) 
at android.os.Parcel.readException(Parcel.java:1552) 
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3550) 
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4778) 
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018) 
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1468) 
at android.content.ContentResolver.query(ContentResolver.java:475) 
at android.content.CursorLoader.loadInBackground(CursorLoader.java:64) 
at android.content.CursorLoader.loadInBackground(CursorLoader.java:56) 
    at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312) 
    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69) 
    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66) 
    at android.os.AsyncTask$2.call(AsyncTask.java:295) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)  
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)  
    at java.lang.Thread.run(Thread.java:818)  09-25 22:57:33.859 28524-28621/com.example.listviewloader D/OpenGLRenderer: Use 
EGL_SWAP_BEHAVIOR_PRESERVED: true 
09-25 22:57:33.863 28524:28524 D/ ] 
HostConnection::get() New Host Connection established 0xaa9e84a0, tid 28524 
    09-25 22:57:34.200 28524-28621/com.example.listviewloader I/OpenGLRenderer: Initialized EGL, version 1.4 
    09-25 22:57:34.910 28524-28621/com.example.listviewloader E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb40943e0 

感谢您对下面的代码中发现任何问题。


import android.app.ListActivity; 
import android.content.CursorLoader; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.support.v4.widget.DrawerLayout; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListView; 
import android.widget.ProgressBar; 
import android.app.LoaderManager; 
import android.content.Loader; 
import android.widget.SimpleCursorAdapter; 
import android.widget.Toast;  

public class MainActivity extends ListActivity 
    implements LoaderManager.LoaderCallbacks<Cursor> { 

// This is the Adapter being used to display the list's data 
SimpleCursorAdapter mAdapter; 

// These are the Contacts rows that we will retrieve 
static final String[] PROJECTION = new String[] {ContactsContract.Data._ID, 
     ContactsContract.Data.DISPLAY_NAME}; 

// This is the select criteria 
static final String SELECTION = "((" + 
     ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" + 
     ContactsContract.Data.DISPLAY_NAME + " != ''))"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  

    // For the cursor adapter, specify which columns go into which views 
    String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME}; 
    int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1 

    // Create an empty adapter we will use to display the loaded data. 
    // We pass null for the cursor, then update it in onLoadFinished() 
    mAdapter = new SimpleCursorAdapter(this, 
      android.R.layout.simple_list_item_1, null, 
      fromColumns, toViews, 0); 
    setListAdapter(mAdapter); 

    // Prepare the loader. Either re-connect with an existing one, 
    // or start a new one. 
    getLoaderManager().initLoader(0, null, this); //this seems to be wrong 
} 

// Called when a new Loader needs to be created 
public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
    // Now create and return a CursorLoader that will take care of 
    // creating a Cursor for the data being displayed. 

    return new CursorLoader(this, ContactsContract.Data.CONTENT_URI, 
      PROJECTION, SELECTION, null, null); 
} 

// Called when a previously created loader has finished loading 
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 
    // Swap the new cursor in. (The framework will take care of closing the 
    // old cursor once we return.) 
    mAdapter.swapCursor(data); 
} 

// Called when a previously created loader is reset, making the data unavailable 
public void onLoaderReset(Loader<Cursor> loader) { 
    // This is called when the last Cursor provided to onLoadFinished() 
    // above is about to be closed. We need to make sure we are no 
    // longer using it. 
    mAdapter.swapCursor(null); 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    // Do something when a list item is clicked 
} 
+0

w你应该发布错误日志和你班级的导入声明。 – Bob

回答

0

的抛出:SecurityException和权限拒绝给出了可能的原因 - 你给你的应用程序访问读取(也可能写)联系人?

您所需要的权限是:

Read access to one or more tables 
<uses-permission android:name="android.permission.READ_CONTACTS">. 
Write access to one or more tables 
<uses-permission android:name="android.permission.WRITE_CONTACTS">. 
+0

我在正确的位置添加了使用权限,但又收到了“由...引发:java.lang.SecurityException:Permission Denial:从ProcessRecord {6722488 2445:com.example.ListView/u0a89打开provider com.android.providers.contacts.ContactsProvider2 }(pid = 2445,uid = 10089)需要android.permission.READ_CONTACTS或android.permission.WRITE_CONTACTS – gnoejh

0

产生的原因:java.lang.SecurityException异常:权限拒绝:开放商 com.android.providers.contacts.ContactsProvider

如果您在Android 6或更高版本上运行您的应用只需确保您在此应用中所需的所有权限均为example