onCreateLoader не должен быть нестатическим классом внутреннего члена - PullRequest
0 голосов
/ 02 сентября 2018

Я учусь использовать AsyncTaskLoader. поэтому я создал простой пример. но когда я запускаю приложение и нажимаю кнопку, приложение падает, и я получаю

onCreateLoader must not be a non-static inner member class

пожалуйста, взгляните на ошибку logcat, опубликованную ниже.

пожалуйста, дайте мне знать, как решить эту проблему

код

public class ActMain extends AppCompatActivity implements LoaderManager.LoaderCallbacks<String>{

private final String TAG = ActMain.class.getSimpleName();
private Button mButtonStart = null;
private TextView mTextView = null;

public static final int CONST_ASYNC_LOADER_ID = 10;
public static final String CONST_BUNDLE_KEY = "bundleKey";
private int i = 0;

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

    this.mButtonStart = (Button) findViewById(R.id.actMainButtonStart);
    this.mTextView = (TextView) findViewById(R.id.actMainTextViewState);

    this.mButtonStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            initAsyncTaskLoader();
        }
    });
}

private void initAsyncTaskLoader() {
    //getSupportLoaderManager().initLoader(CONST_ASYNC_LOADER_ID, null, this);
    Bundle queryBundle = new Bundle();
    // Use putString with OPERATION_QUERY_URL_EXTRA as the key and the String value of the URL as the value
    queryBundle.putString(CONST_BUNDLE_KEY,"text_" + (++i));
    // Call getSupportLoaderManager and store it in a LoaderManager variable
    LoaderManager loaderManager = getSupportLoaderManager();
    // Get our Loader by calling getLoader and passing the ID we specified
    Loader<String> loader = loaderManager.getLoader(CONST_ASYNC_LOADER_ID);
    // If the Loader was null, initialize it. Else, restart it.
    if(loader==null) {
        loaderManager.initLoader(CONST_ASYNC_LOADER_ID, queryBundle, this);
    } else{
        loaderManager.restartLoader(CONST_ASYNC_LOADER_ID, queryBundle, this);
        Log.i(TAG, "--------->restartLoader<--------");
        Log.i(TAG, "--------->loader<--------: " + loader);
    }
}

@Override
public Loader<String> onCreateLoader(int id, Bundle args) {
    Log.w(TAG, "@onCreateLoader id: " + id);
    Log.w(TAG, "@onCreateLoader args: " + args);

    return new android.support.v4.content.AsyncTaskLoader<String>(this) {
        @Override
        public String loadInBackground() {
            Log.w(TAG, "@loadInBackground");

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "FINISHED";
        }
    };
}

@Override
public void onLoadFinished(Loader<String> loader, String data) {
    Log.w(TAG, "@onLoadFinished");
}

@Override
public void onLoaderReset(Loader<String> loader) {
    Log.w(TAG, "@onLoadFinished");
}
}

ошибка

java.lang.IllegalArgumentException: Object returned from onCreateLoader must not be a non-static inner member class: Loader{2da0bab0 id=0}
at android.support.v4.app.LoaderManagerImpl$LoaderInfo.start(LoaderManager.java:261)
at android.support.v4.app.LoaderManagerImpl.installLoader(LoaderManager.java:562)
at android.support.v4.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:549)
at android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:603)
asynctaskloaderwithbuttons.ActMain.initAsyncTaskLoader(ActMain.java:57)
asynctaskloaderwithbuttons.ActMain.access$000(ActMain.java:18)
asynctaskloaderwithbuttons.ActMain$1.onClick(ActMain.java:41)
...