【无法解析Android中的方法'
execute()'
】登山则情满于山,观海则意溢于海。这篇文章主要讲述无法解析Android中的方法'
execute()'
相关的知识,希望能为你提供帮助。
我试图在我的一个Circular ProgressBar
中实施fragment
。
我希望使用AsyncTask
类以及其他后台进程来实现这一点,我希望progress bar
也能更新onProgressUpdate()
方法。
这是我的Fragment实现ProgressBar:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.playlist_fragment, container, false);
// Setting up progressBar
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
progressBar.setProgress(0);
return rootView;
}
从Fragment中调用
AsyncTask
类,它说无法在setProgressBr()
解析方法。我想我没有适当地传递参数。new GetPlaylistAsyncTask(mYouTubeDataApi,getContext(),PlaylistFragment.this)
.setProgressBar(progressBar)
.execute(playlistVideos.playlistId, playlistVideos.getNextPageToken());
这是我的AsyncTask类:
public class GetPlaylistAsyncTask extends AsyncTask<
String, Integer, Pair<
String, List<
Video>
>
>
{
private static final String TAG = "GetPlaylistAsyncTask";
private static final Long YOUTUBE_PLAYLIST_MAX_RESULTS = 10L;
//see: https://developers.google.com/youtube/v3/docs/playlistItems/list
private static final String YOUTUBE_PLAYLIST_PART = "snippet";
private static final String YOUTUBE_PLAYLIST_FIELDS = "pageInfo,nextPageToken,items(id,snippet(resourceId/videoId))";
//see: https://developers.google.com/youtube/v3/docs/videos/list
private static final String YOUTUBE_VIDEOS_PART = "snippet,contentDetails,statistics";
// video resource properties that the response will include.
private static final String YOUTUBE_VIDEOS_FIELDS = "items(id,snippet(title,description,thumbnails/high),contentDetails/duration,statistics)";
// selector specifying which fields to include in a partial response.private YouTube mYouTubeDataApi;
Context mContext;
ProgressBar bar;
private AsyncResponse theListener;
public void setProgressBar(ProgressBar bar) {
this.bar = bar;
}public GetPlaylistAsyncTask(YouTube api, Context context, PlaylistFragment frag ) {
mYouTubeDataApi = api;
mContext = context;
theListener = (AsyncResponse)frag;
}@Override
protected Pair<
String, List<
Video>
>
doInBackground(String... params) {
final String playlistId = params[0];
final String nextPageToken;
if (params.length == 2) {
nextPageToken = params[1];
} else {
nextPageToken = null;
}PlaylistItemListResponse playlistItemListResponse;
try {
playlistItemListResponse = mYouTubeDataApi.playlistItems()
.list(YOUTUBE_PLAYLIST_PART)
.setPlaylistId(playlistId)
.setPageToken(nextPageToken)
.setFields(YOUTUBE_PLAYLIST_FIELDS)
.setMaxResults(YOUTUBE_PLAYLIST_MAX_RESULTS)
.setKey(ApiKey.YOUTUBE_API_KEY)
.execute();
} catch (IOException e) {
e.printStackTrace();
return null;
}if (playlistItemListResponse == null) {
Log.e(TAG, "Failed to get playlist");
return null;
}List<
String>
videoIds = new ArrayList();
// pull out the video id's from the playlist page
for (PlaylistItem item : playlistItemListResponse.getItems()) {
videoIds.add(item.getSnippet().getResourceId().getVideoId());
}// get details of the videos on this playlist page
VideoListResponse videoListResponse = null;
try {
videoListResponse = mYouTubeDataApi.videos()
.list(YOUTUBE_VIDEOS_PART)
.setFields(YOUTUBE_VIDEOS_FIELDS)
.setKey(ApiKey.YOUTUBE_API_KEY)
.setId(TextUtils.join(",", videoIds)).execute();
} catch (IOException e) {
e.printStackTrace();
}return new Pair(playlistItemListResponse.getNextPageToken(), videoListResponse.getItems());
}@Override
protected void onPreExecute() {}@Override
protected void onProgressUpdate(Integer... values) {
if (this.bar != null) {
bar.setProgress(values[0]);
}
}@Override
protected void onPostExecute(Pair<
String, List<
Video>
>
result) {
theListener.handleGetPlaylistResult(null,result);
}
}
将欣赏上述问题的快速解决方案。提前致谢。
答案
.setProgressBar(progressBar)
方法的返回类型为void,因此您无法像这样链接调用new GetPlaylistAsyncTask(mYouTubeDataApi,getContext(),PlaylistFragment.this)
.setProgressBar(progressBar)
.execute(playlistVideos.playlistId, playlistVideos.getNextPageToken());
将您的
setProgressBar()
方法更改为以下内容,它应该可以正常工作public GetPlaylistAsyncTask setProgressBar(ProgressBar bar) {
this.bar = bar;
return this;
}
要么
如果您不想更改方法签名,请执行以下操作
final GetPlaylistAsyncTask getPlaylistAsyncTask = new GetPlaylistAsyncTask(mYouTubeDataApi,getContext(),PlaylistFragment.this);
getPlaylistAsyncTask.setProgressBar(progressBar);
getPlaylistAsyncTask.execute(playlistVideos.playlistId, playlistVideos.getNextPageToken());
同时确保两个参数
playlistVideos.playlistId, playlistVideos.getNextPageToken()
都是字符串。另一答案你可以这样做
GetPlaylistAsyncTask playAsync=new (mYouTubeDataApi,getContext(),PlaylistFragment.this);
playAsync.setProgressBar(progressBar);
playAsync.execute(playlistVideos.playlistId, playlistVideos.getNextPageToken());
希望这对你有用。
另一答案你正在调用链中的方法。这样,每个方法都应该返回其调用的当前实例。修改您的方法,如下所示它将起作用。
public GetPlaylistAsyncTask setProgressBar(ProgressBar bar) {
this.bar = bar;
return this;
}
另一答案您无法直接将进度添加到循环进度条,以下是我为解决此问题所做的工作。
添加一个drawable文件circular_progress.xml
<
?xml version="1.0" encoding="utf-8"?>
<
item android:id="@android:id/secondaryProgress">
<
shape
android:innerRadius ="15dp"
android:shape="ring"
android:useLevel="true"
android:thickness="4dp" >
<
gradient
android:endColor="@color/blackBackground"
android:startColor="@color/blackBackground"
android:type="sweep" />
<
/shape>
<
/item>
<
item android:id="@android:id/progress">
<
rotate
android:fromDegrees="270"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="270" >
<
shape
android:innerRadius ="15dp"
android:shape="ring"
android:useLevel="true"
android:thickness="4dp">
<
rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
<
gradient
android:endColor="#e1c25b"
android:startColor="#e1c25b"
android:type="sweep" />
<
/shape>
<
/rotate>
<
/item>
在片段布局中添加进度条
<
ProgressBar
android:id="@+id/pb_download"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="100"
android:progress="75"
android:progressDrawable="@drawable/circular_progress" />
现在您可以使用setProgress()来更新进度。随意问任何疑问。
推荐阅读
- Android JobScheduler需要先检查是否正常工作,然后再运行其他任务
- 通过在Android中实现Proguard,使用JDBC在Asynctask中崩溃
- Android Studio - 对象构造函数中的NetworkOnMainThreadException
- 无法将值传递给Android中的AsyncTask
- AsyncTask Android方法的技术差异
- Android AsyncTask问题(连接到Web服务)
- Android(Kotlin) - 如何等待异步任务完成()
- E / AndroidRuntime(致命异常:Android的AsyncTask#1 ERROR)
- Android AsyncTask无法运行