无法解决方法' execute()'在Android中

时间:2017-12-22 05:32:17

标签: android android-fragments android-asynctask android-progressbar

我正在尝试在Circular ProgressBar。{/ p>之一中实施fragment

我希望使用AsyncTask类以及其他后台进程来实现此目标,并且我希望progress bar也可以在onProgressUpdate()方法上进行更新。

这是我的片段实施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);
    }
}

欢迎快速解决上述问题。提前谢谢。

4 个答案:

答案 0 :(得分:3)

.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;
}

OR

如果您不想更改方法签名,请执行以下操作

final GetPlaylistAsyncTask getPlaylistAsyncTask = new GetPlaylistAsyncTask(mYouTubeDataApi,getContext(),PlaylistFragment.this);
getPlaylistAsyncTask.setProgressBar(progressBar);
getPlaylistAsyncTask.execute(playlistVideos.playlistId, playlistVideos.getNextPageToken());

同时确保两个参数playlistVideos.playlistId, playlistVideos.getNextPageToken()都是字符串。

答案 1 :(得分:0)

你可以这样做

GetPlaylistAsyncTask playAsync=new (mYouTubeDataApi,getContext(),PlaylistFragment.this);
playAsync.setProgressBar(progressBar);
playAsync.execute(playlistVideos.playlistId, playlistVideos.getNextPageToken());

希望这对你有用。

答案 2 :(得分:0)

您正在调用链中的方法。这样,每个方法都应该返回其调用的当前实例。修改您的方法,如下所示。

public GetPlaylistAsyncTask setProgressBar(ProgressBar bar) {
this.bar = bar;
return this;
}

答案 3 :(得分:0)

您无法直接将进度添加到循环进度条,以下是我为解决此问题所做的工作。

添加一个可绘制的文件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()来更新进度。 随意问任何疑问。