Android Studio 标准列表视图:E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1

Posted

技术标签:

【中文标题】Android Studio 标准列表视图:E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1【英文标题】:Android studio standard listview: E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 【发布时间】:2018-10-30 13:04:04 【问题描述】:

我正在尝试获取一个简单的列表视图,并遵循https://developer.android.com/guide/topics/ui/layout/listview 上的标准列表视图。 只有当我按下 MainActivity 中的按钮时,应用程序才会关闭,并且我会收到您可以在 logcat 中看到的消息。我刚开始编写java并使用android studio,所以我做的事情可能是完全错误的。有谁知道我的问题是什么以及如何解决它?

我的主要活动:

package com.wouterdesigns.a8383;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity 

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = findViewById(R.id.switchbutton);
    button.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            switchToListView();
        
    );


public void switchToListView()
    Intent i = new Intent(this, ListViewLoader.class);
    startActivity(i);

我的 ListViewLoader:

package com.wouterdesigns.a8383;

import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;

public class ListViewLoader 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);

    // Create a progress bar to display while the list loads
    ProgressBar progressBar = new ProgressBar(this);
    progressBar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
    progressBar.setIndeterminate(true);
    getListView().setEmptyView(progressBar);

    // Must add the progress bar to the root of the layout
    ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
    root.addView(progressBar);

    // 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);


// 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

我的日志:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.wouterdesigns.a8383, PID: 13242
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:365)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:784)
 Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord866b18b 13242:com.wouterdesigns.a8383/u0a209 (pid=13242, uid=10209) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
    at android.os.Parcel.readException(Parcel.java:1945)
    at android.os.Parcel.readException(Parcel.java:1891)
    at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4779)
    at android.app.ActivityThread.acquireProvider(ActivityThread.java:6645)
    at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2798)
    at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1820)
    at android.content.ContentResolver.query(ContentResolver.java:750)
    at android.content.ContentResolver.query(ContentResolver.java:716)
    at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
    at android.content.CursorLoader.loadInBackground(CursorLoader.java:54)
    at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:315)
    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:64)
    at android.os.AsyncTask$2.call(AsyncTask.java:345)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
    at java.lang.Thread.run(Thread.java:784)

【问题讨论】:

您是否在 Manifest 文件中请求了对“android.permission.READ_CONTACTS”的权限。 --您的错误是在您尚未发布的AsyncTask 中引起的,因为您不需要“android.permission.READ_CONTACTS”的权限 我的清单文件中有这一行:'' 在执行访问数据的代码之前,您需要确定用户确实授予了权限 哇哇...我觉得自己是个白痴xD。它从来没有征求过许可,所以我认为一切都很好。在设置菜单中,我允许该应用读取我的联系人,并且一切正常!非常感谢。 【参考方案1】:

如果您针对 Android M (targetSdkVersion23) 进行测试,您必须在运行时请求权限。例如,这些主题已经回答了这个问题:

Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord in Android Studio

【讨论】:

以上是关于Android Studio 标准列表视图:E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio [RecyclerView/列表视图]

Android Studio:如何将列表视图中的项目添加到列表视图 onClick

列表视图在 android studio 中没有显示任何值

通过单击列表视图上的列表项更改 MapView Android Studio 上的位置相机视图

长按列表视图后的操作(Android Studio)

在 Android Studio 中向列表视图添加自定义行