programing tip

getLoaderManager (). initLoader ()는 클래스 (ListFragment)가 LoaderManager.LoaderCallbacks를 구현하지만 'this'를 인수로 허용하지 않습니다.

itbloger 2020. 11. 23. 07:54
반응형

getLoaderManager (). initLoader ()는 클래스 (ListFragment)가 LoaderManager.LoaderCallbacks를 구현하지만 'this'를 인수로 허용하지 않습니다.


Android에서 SQLite 사용대한 가이드따르는 데 문제 가 있습니다. (예제에서와 같이) ListFragment대신 a 사용하고 ListActivity있으므로 대신 ListFragment구현이 LoaderManager.LoaderCallbacks<Cursor>있습니다. 그런 다음의 fillData()방법에서 ListFragment:

private void fillData() {
    // Fields from the database (projection)
    // Must include the _id column for the adapter to work
    String[] from = new String[] { NotesSQLiteHelper.COLUMN_TITLE };
    // Fields on the UI to which we map
    int[] to = new int[] { R.id.label };

    getLoaderManager().initLoader(0, null, this); //error
    adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.notes_row, null, from, to, 0);
    setListAdapter(adapter);
}

오류가 발생합니다.

The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, NotesActivity.ArrayListFragment)

this구현 하더라도 표시된 줄에 LoaderManager.LoaderCallbacks<Cursor>.

어떤 아이디어라도 감사합니다.


CursorLoader의 올바른 구현을 사용하고 있지 않습니다 Loader. 이전 가져 오기를 제거하고 다음을 사용하십시오.

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

그러나 SherlockActionBar를 사용하여 동일한 문제가 있습니다. 확장해야하므로 SherlockListActivityNO method가 getSupportLoadManager()있습니다.

이것에 대한 아이디어가 있습니까?

편집 : 조각을 사용하는 방법을 모르는 경우이 자습서를 따르십시오 . extends SherlockFragment를 사용하여 새 클래스를 만들고 디스플레이 로직을 여기로 이동하십시오. 이전 활동이 SherlockFragmentActivity를 확장하고 새로 생성 된 SherlockFragment를 표시하도록합니다. 이런 식으로 작동합니다. @JakeWharton에게 감사드립니다!


주의해야 할 몇 가지 사항 (최근이 문제를 겪은 경험에서) :

  1. minSDK가 11 미만으로 설정되어 있고 (예 : Gingerbread의 경우 레벨 10) 이전 버전과의 호환성을 위해 지원 팩을 사용하는 경우

    getSupportLoaderManager().initLoader(LOADER_ID, null, this);
    
  2. ListFragment를 사용하고 있다고 말했을 때 언급했지만 반복됩니다. 활동을 확장하지 마십시오. 그렇지 않으면 지원 패키지가 작동하지 않습니다. 대신 FragmentActivity 클래스 또는 ListFragment 클래스를 확장하십시오.

  3. 가져 오기의 경우 minSDK가 11 미만인 경우 올바른 버전을 사용하고 있는지 확인하십시오.

     android.support.v4.app.FragmentActivity;
     android.support.v4.app.LoaderManager;
     android.support.v4.content.Loader;
    

이것이 당신이나 적어도 다른 누군가에게 도움이되기를 바랍니다.


세 번째 인수를 캐스팅하면 내 경우 문제가 해결되었습니다.

...에서

 getLoaderManager().initLoader(0, null, this);

...에

 getLoaderManager().initLoader(0, null, (android.app.LoaderManager.LoaderCallbacks<Cursor>) this);

노트 :

  1. minSdk는 8이었고 지원 라이브러리 v4를 사용했습니다.
  2. (android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>) this) 작동하지 않았다.
  3. getSupportLoaderManager() or getSupportLoadManager() 작동하지 않았다.
  4. 이 코드는 조각이 아닌 활동 내부에있었습니다.

늦었지만 도움이 될 수도 있습니다.
조각에서 로더 관리자를 사용하고 최소 API가 HONEYCOMB 미만이면
이 가져 오기를 사용하십시오.

import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;      
import android.support.v4.app.Fragment;

로더를 시작하려면이 코드를 사용하십시오.

getActivity().getSupportLoaderManager().initLoader(/loader stuff*/);

이것이 도움이되기를 바랍니다.


수입품을 다음으로 변경하십시오.

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

그리고 사용

getSupportLoaderManager().initLoader(0, null, this);

내 오류는 다음과 같습니다.

//import android.app.ListFragment; Error when doesnt import from support.v4
import android.support.v4.app.ListFragment;

제 경우에는 활동이 android.support.v7.app 패키지에서 ActionBarActivity를 확장해야했습니다. 그때 나는 사용할 수 있었다

getSupportLoaderManager (). initLoader (0, null, this);


나는 내 앱과 함께 ActionBarSherlock을 사용하고 있으며이 문제에 직면 하고이 질문에서 다른 사람들이 논의한 모든 단계를 수행했습니다. 그러나 제안 된 모든 해결 방법을 시도한 후에도 동일한 문제가 계속 발생했습니다.

The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, this)

문제는 이클립스가 내가 뭔가를 놓쳤을 때 내게 말해 줄 것을 기대하는 것에 빠졌고 그것은 내가 익숙한 방식이 아닌 나에게 말하고 있었다. 일반적으로 Eclipse는 다른 경우에 뭔가 작동하도록 재정의가 누락되었다고 말하지만 여기서 직접 말하는 것은 아닙니다. 마침내 "LoaderManager.LoaderCallbacks"가 문제가되었고 이에 대한 콜백이 없다는 것을 깨달았습니다. 따라서이 오류는 실제로 매우 유효한 오류였습니다. 기본 재정의를 추가하면 문제가 해결되었고 앞으로 나아갈 수있었습니다.

// Creates a new loader after the initLoader () call
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
  // do work
  return null;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
  adapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
  // data is not available anymore, delete reference
  adapter.swapCursor(null);
}

I was having a similar issue where my AsyncTaskLoader was not returning my List, but my resolution was to change the call from .initLoader to .restartLoader.

So I actually never called .initLoader and just immediately called .restartLoader.

I had a onListItemClick listener in my fragment and every time backed out of the fragment and reloaded the onListItemClick and navigated through the app it kept crashing.

It might just be specific to my app but hopefully it helps others if they are having fragment backstack reload issues.

This was part of a file manager portion in my app so it is specific to clicking multiple onListItemClicks in the fragment you are loading.


I got around this by using a SherlockListFragment (or you could use ListFragment, I suppose), but have it contained within a Activity. I first created a generic FragmentHolderActivity class that looks like this:

FragmentHolderActivity.java

public class FragmentHolderActivity extends SherlockFragmentActivity {

    public static final String FRAGMENT_LAYOUT_RESOURCE_ID = "fragment_layout_resource_id";

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

        Integer fragmentLayoutResourceId = getIntent().getIntExtra(FRAGMENT_LAYOUT_RESOURCE_ID, Integer.MAX_VALUE);
        Assert.assertNotSame(fragmentLayoutResourceId, Integer.MAX_VALUE);

        setContentView(fragmentLayoutResourceId);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return false;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

Then you need to create an XML file for your fragment.

your_list_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.example.YourListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment" />

And here's the code you use to start the fragment:

    Intent intent = new Intent();
    intent.setClass(this, FragmentHolderActivity.class);
    intent.putExtra(FragmentHolderActivity.FRAGMENT_LAYOUT_RESOURCE_ID, R.layout.your_list_fragment);
    startActivity(intent);

You just tell the FragmentHolderActivity to use the your_list_fragment layout, which in turn loads the YourListFragment.java

You can then use: getSherlockActivity().getSupportLoaderManager().initLoader(...) in YourListFragment.java

Not sure if this is the correct approach, but it keeps all my logic in Fragments, which is nice.


Ran into the same problem. My minSdkVersion is 14, so cannot use android.support.v4 package.

I figured it by extending LoaderCallbacks, instead of LoaderManager.LoaderCallbacks and use these packages

import android.app.LoaderManager.LoaderCallbacks;  
import android.content.CursorLoader;  
import android.database.Cursor;  
import android.widget.SimpleCursorAdapter;

If you have tried all the above methods and still facing the same error with the "this" parameter, then follow these steps :

  1. Go to settings and enable imports on the fly option.(by default it'll be enabled and you can do this by using Alt+Enter keys).

  2. Cut the whole code of the activity which had implemented
    LoaderCallback... and paste it in a text editor.

  3. Then at last copy the whole code of that activity from the text editor where you had pasted, without any import commands.( Only the code of the class/activity).

  4. You'll get lots of errors as you have imported nothing yet. Just press Alt+Enter wherever you're getting the errors and it's libraries will be imported automatically. Note : Choose android.app... library for CursorLoaders.


Try these 2 lines it will work

android.support.v4.app.LoaderManager loaderManager = getSupportLoaderManager();

loaderManager.initLoader(LOADER_ID, null,  this);

I am using minSDK 27 and had this same issue, I tried to cast like @Dexter suggest, but that gave an error saying cannot be cast to android.app.LoaderManager$LoaderCallbacks, so I then tried to use different import statements and this worked. I commented out the v4 versions and this is what I have now and the app is working:

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

Not quite sure when/how the v4 versions were imported.


0

After a long sacrifice i got this solution if you are using fragments then just use this code.

getActivity().getSupportLoaderManager().initLoader(1, null, YourActivity.this);

i hope this is helpful for you

참고 URL : https://stackoverflow.com/questions/11155753/getloadermanager-initloader-doesnt-accept-this-as-argument-though-the-cla

반응형