在 Activity Android 中实施 LoaderCallbacks

Implementing LoaderCallbacks in Activity Android

我是 android 的新人。我想使用 loaderCallbacks 获取 contacts 数据 我写了一些代码,但这里有一些问题,我不知道为什么会这样 可以看看吗

package com.example.arfan.myfirstapp;

import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportLoaderManager().initLoader(1, null, this); // ERROR IS HERE in 3rd Argument
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @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 Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri CONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        return new CursorLoader(this, CONTENT_URI, null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        Log.i("INFO","This is working");
        while(data.moveToNext()){
            Log.i("Cantacts : ",data.getString(data.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}

this 参数中存在某种问题。如果我删除此行,则不会发生错误,也不会找到任何结果。我从一个 网站如何获取 android 中的联系人数据,您可以在此处访问此网站 Go to Site

能否请您告诉我问题出在哪里以及如何解决。 谢谢。

更新: 控制台显示此内容。

Error:(25, 34) error: method initLoader in class LoaderManager cannot be applied to given types;
required: int,Bundle,LoaderCallbacks<D>
found: int,<null>,MainActivity
reason: cannot infer type-variable(s) D
(argument mismatch; MainActivity cannot be converted to LoaderCallbacks<D>)
where D is a type-variable:
D extends Object declared in method <D>initLoader(int,Bundle,LoaderCallbacks<D>)

由于您正在使用 AppCompatActivity 并调用 getSupportLoaderManager(),我想您也想使用 Loader 的支持库实现。

如果是,请更改您的 import 语句的以下内容:

import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;

为此:

import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

如果不这样做,请调用 getLoaderManager() 而不是 getSupportLoaderManager()