列表视图中的文件下载按钮

时间:2019-05-17 10:15:37

标签: android-layout android-listview

我想在php服务器的文件列表视图中创建一个文件下载按钮。

因此,我制作了一个列表视图表格,并从连接到php,db的网站上获取了上传的文件列表。

但是,当我在mainactivity.java中使用按钮onclick侦听器时,该应用程序将关闭。

Logcat说onclicklistener错误。

我认为这就是为什么我使用两个URL连接服务器的原因。 例如,一个网址用于上传的文件列表,一个网址用于下载按钮。

fido.xml是列表视图的形式。 在fido.xml中,有一个按钮和许多文本视图。(文件标题,上载日期,发布者和内容等) 我还制作了FiDoListAdapter.java,FiDo.java来从服务器获取上传的文件列表。 另外,我还允许Internet上使用清单。

公共类MainActivity扩展了AppCompatActivity {

Button button;
DownloadManager downloadManager;

private ListView fileListView;
private FiDoListAdapter adapter;
private List<FiDo> fiDoList;

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

    button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
            Uri uri = Uri.parse("http://****/*******/upload/201905/2019-05-16_01.56.54.png");
            DownloadManager.Request request = new DownloadManager.Request(uri);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            Long reference = downloadManager.enqueue(request);
        }
    });


    fileListView = (ListView)findViewById(R.id.fileListView);
    fiDoList = new ArrayList<FiDo>();

    adapter = new FiDoListAdapter(getApplicationContext(), fiDoList);
    fileListView.setAdapter(adapter);

    new BackgroundTask().execute();
}

class BackgroundTask extends AsyncTask<Void, Void, String> {
    String target;

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        target = "http://*********.php";
    }

    @Override
    protected String doInBackground(Void... voids) {
        try{
            URL url = new URL(target);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String temp;
            StringBuilder stringBuilder = new StringBuilder();

            while((temp = bufferedReader.readLine()) != null){
                stringBuilder.append(temp + "\n");
            }

            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();
        }
        catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values){
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result){
        super.onPostExecute(result);
        try{
            JSONObject jsonObject = new JSONObject(result);
            JSONArray jsonArray = jsonObject.getJSONArray("response");
            int count = 0;

            String title, content, a_id, date;

            while(count < jsonArray.length()){
                JSONObject object = jsonArray.getJSONObject(count);
                title = object.getString("title");
                content = object.getString("content");
                a_id = object.getString("a_id");
                date = object.getString("date");

                FiDo notice = new FiDo(title, content, a_id, date);
                fiDoList.add(notice);
                adapter.notifyDataSetChanged();
                count++;
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

}

这是日志猫。

---------崩溃开始 2019-05-17 18:46:02.603 21303-21303 / com.example.postdown E / AndroidRuntime:FATAL EXCEPTION:main     流程:com.example.postdown,PID:21303     java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.postdown / com.example.postdown.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android。 view.View $ OnClickListener)'上的空对象引用         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)         在android.app.ActivityThread.-wrap14(ActivityThread.java)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1631)         在android.os.Handler.dispatchMessage(Handler.java:102)         在android.os.Looper.loop(Looper.java:154)         在android.app.ActivityThread.main(ActivityThread.java:6682)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)      原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'         在com.example.postdown.MainActivity.onCreate(MainActivity.java:38)         在android.app.Activity.performCreate(Activity.java:6942)         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)         在android.app.ActivityThread.-wrap14(ActivityThread.java)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1631)         在android.os.Handler.dispatchMessage(Handler.java:102)         在android.os.Looper.loop(Looper.java:154)         在android.app.ActivityThread.main(ActivityThread.java:6682)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

0 个答案:

没有答案