此回调中的 getLoaderManager().initLoader 错误
getLoaderManager().initLoader error in this callback
第一次使用Loader。调用方法后
getLoaderManager().initLoader(0, null, this);
我在参数“this”中遇到错误。
编译后出错:
error: method initLoader in class LoaderManager cannot be applied to given types;
getLoaderManager().initLoader(0, null, this);
^
required: int,Bundle,LoaderCallbacks
found: int,,MainActivity
reason: cannot infer type-variable(s) D
(argument mismatch; MainActivity cannot be converted to LoaderCallbacks)
where D is a type-variable:
D extends Object declared in method initLoader(int,Bundle,LoaderCallbacks)
我的代码:
package com.example.newsapp;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<News>> {
private static final String URL = "http://content.guardianapis.com/search";
private NewsAdapter newsAdapter;
private TextView emptyListStateTextView;
private Intent intent;
ListView newsListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.e("MainActivity", "RUN onCreate...");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newsListView = findViewById(R.id.listView);
emptyListStateTextView = findViewById(R.id.defautView);
newsListView.setEmptyView(emptyListStateTextView);
newsAdapter = new NewsAdapter(this, new ArrayList<News>());
newsListView.setAdapter(newsAdapter);
newsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
News currentNews = newsAdapter.getItem(position);
Uri uri = Uri.parse(currentNews.getUrl());
intent= new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null) {
View loadingIndicator = findViewById(R.id.loading_bar);
loadingIndicator.setVisibility(View.GONE);
emptyListStateTextView.setText("No internet connection");
}else{
getLoaderManager().initLoader(0, null, this);
}
Log.e("MainActivity", "DONE onCreate...");
}
@NonNull
@Override
public Loader<List<News>> onCreateLoader(int i, Bundle args) {
Log.e("MainActivity", "RUN onCreateLoader...");
//TEST
Uri baseUri = Uri.parse(URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendQueryParameter("show-tags", "contributor");
uriBuilder.appendQueryParameter("section", "technology");
uriBuilder.appendQueryParameter("order-by", "newest");
uriBuilder.appendQueryParameter("from-date", "2018-01-01");
uriBuilder.appendQueryParameter("api-key", "test");
Log.v("my_tag", "url created is: "+uriBuilder.toString());
//TEST
Log.e("MainActivity", "DONE onCreateLoader...");
return new NewsLoader(this, uriBuilder.toString());
}
@Override
public void onLoadFinished(@NonNull Loader<List<News>> loader, List<News> data) {
Log.e("MainActivity", "RUN onLoadFinished...");
View loadingBar = findViewById(R.id.loading_bar);
loadingBar.setVisibility(View.GONE);
newsAdapter.clear();
if(data != null && !data.isEmpty()){
newsAdapter.addAll(data);
}
Log.e("MainActivity", "DONE onLoadFinished...");
}
@Override
public void onLoaderReset(@NonNull Loader<List<News>> loader) {
Log.e("MainActivity", "RUN onLoaderReset...");
newsAdapter.clear();
Log.e("MainActivity", "DONE onLoaderReset...");
}
}
我个人认为,问题一定出在导入的某个地方。即使我真的很感激任何帮助。
谢谢!
getLoaderManager
已贬值,请使用 getSupportLoaderManager
代替:
getSupportLoaderManager().initLoader(0, null, this);
但是,如果您仍然想使用它,那是因为导入不正确。导入加载程序回调,它应该可以解决问题
android.app.LoaderCallbacks
第一次使用Loader。调用方法后
getLoaderManager().initLoader(0, null, this);
我在参数“this”中遇到错误。
编译后出错:
error: method initLoader in class LoaderManager cannot be applied to given types; getLoaderManager().initLoader(0, null, this); ^ required: int,Bundle,LoaderCallbacks found: int,,MainActivity reason: cannot infer type-variable(s) D (argument mismatch; MainActivity cannot be converted to LoaderCallbacks) where D is a type-variable: D extends Object declared in method initLoader(int,Bundle,LoaderCallbacks)
我的代码:
package com.example.newsapp;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<News>> {
private static final String URL = "http://content.guardianapis.com/search";
private NewsAdapter newsAdapter;
private TextView emptyListStateTextView;
private Intent intent;
ListView newsListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.e("MainActivity", "RUN onCreate...");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newsListView = findViewById(R.id.listView);
emptyListStateTextView = findViewById(R.id.defautView);
newsListView.setEmptyView(emptyListStateTextView);
newsAdapter = new NewsAdapter(this, new ArrayList<News>());
newsListView.setAdapter(newsAdapter);
newsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
News currentNews = newsAdapter.getItem(position);
Uri uri = Uri.parse(currentNews.getUrl());
intent= new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null) {
View loadingIndicator = findViewById(R.id.loading_bar);
loadingIndicator.setVisibility(View.GONE);
emptyListStateTextView.setText("No internet connection");
}else{
getLoaderManager().initLoader(0, null, this);
}
Log.e("MainActivity", "DONE onCreate...");
}
@NonNull
@Override
public Loader<List<News>> onCreateLoader(int i, Bundle args) {
Log.e("MainActivity", "RUN onCreateLoader...");
//TEST
Uri baseUri = Uri.parse(URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendQueryParameter("show-tags", "contributor");
uriBuilder.appendQueryParameter("section", "technology");
uriBuilder.appendQueryParameter("order-by", "newest");
uriBuilder.appendQueryParameter("from-date", "2018-01-01");
uriBuilder.appendQueryParameter("api-key", "test");
Log.v("my_tag", "url created is: "+uriBuilder.toString());
//TEST
Log.e("MainActivity", "DONE onCreateLoader...");
return new NewsLoader(this, uriBuilder.toString());
}
@Override
public void onLoadFinished(@NonNull Loader<List<News>> loader, List<News> data) {
Log.e("MainActivity", "RUN onLoadFinished...");
View loadingBar = findViewById(R.id.loading_bar);
loadingBar.setVisibility(View.GONE);
newsAdapter.clear();
if(data != null && !data.isEmpty()){
newsAdapter.addAll(data);
}
Log.e("MainActivity", "DONE onLoadFinished...");
}
@Override
public void onLoaderReset(@NonNull Loader<List<News>> loader) {
Log.e("MainActivity", "RUN onLoaderReset...");
newsAdapter.clear();
Log.e("MainActivity", "DONE onLoaderReset...");
}
}
我个人认为,问题一定出在导入的某个地方。即使我真的很感激任何帮助。
谢谢!
getLoaderManager
已贬值,请使用 getSupportLoaderManager
代替:
getSupportLoaderManager().initLoader(0, null, this);
但是,如果您仍然想使用它,那是因为导入不正确。导入加载程序回调,它应该可以解决问题
android.app.LoaderCallbacks