使用带有回收站视图的下载管理器

时间:2016-08-24 12:12:49

标签: java android json android-recyclerview android-download-manager

这是我的DataAdapter类:



package com.example.ashish.fileserver;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import retrofit2.Callback;

/**
 * Created by ashish on 23/8/16.
 */
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private ArrayList<AndroidVersions> android;
    private static String url;
    private Context context;




    public DataAdapter(Context context,ArrayList<AndroidVersions> android) {
        this.android = android;
        this.context = context;

    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {

        viewHolder.tv_name.setText(android.get(i).getName());
        viewHolder.tv_version.setText(android.get(i).getVer());

        //viewHolder.tv_api_level.setText(android.get(i).getApi());
    }


    @Override
    public int getItemCount() {
        return android.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView tv_name,tv_version,tv_api_level;
        public CardView card;
        ArrayList<AndroidVersions> android = new ArrayList<AndroidVersions>();

        public ViewHolder(View view) {
            super(view);
            this.android = android;


            tv_name = (TextView)view.findViewById(R.id.tv_name);
            tv_version = (TextView)view.findViewById(R.id.tv_version);
            view.setOnClickListener(this);

            //tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);

        }


        @Override

        public void onClick(View view) {
            System.out.println(getAdapterPosition());
            int position = getAdapterPosition();
            if (position == 1){
                System.out.println("hey Ashish");
                url = "http://vfu.bg/en/e-Learning/Computer-Basics--CTE_I__-_Computer_Basics.ppt";

                String servicestring = Context.DOWNLOAD_SERVICE;
                DownloadManager downloadmanager;
                downloadmanager  = (DownloadManager)getSystemService(servicestring);
                Uri uri = Uri
                        .parse(url);
                DownloadManager.Request request = new DownloadManager.Request(uri);
                long reference = downloadmanager.enqueue(request);
                Toast.makeText(context.getApplicationContext(),
                        "Your file is now downloading...", Toast.LENGTH_LONG).show();
            }
            
            }




    }


}
&#13;
&#13;
&#13;

我想在点击Cardview时下载一些文件,但它显示java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference的错误。当我点击卡片视图时,它显示App Stopped Working。我怎么能解决这个问题?

2 个答案:

答案 0 :(得分:0)

如果您选中DownloadManager,则会显示:

  

请注意,应用程序必须具有INTERNET权限才能使用   这堂课。

你很擅长吗?此外,这种操作应该在不同的AsyncTask中完成,这样你就可以保持你的主线程免费。

最后,您不会使用System.out.println获取任何日志。请改用Android Log API

答案 1 :(得分:0)

    String servicestring = Context.DOWNLOAD_SERVICE; 
    downloadmanager  = (DownloadManager)getSystemService(servicestring);

错误出现在这两行中,我已将其更改为:

   String servicestring = Context.DOWNLOAD_SERVICE;
   downloadmanager  = (DownloadManager)context.getSystemService(servicestring);

在主要活动中,这一行显示错误:

    adapter = new DataAdapter(data);

为:

    adapter = new DataAdapter(data, ScrollingActivity_Test.this);