gpt4 book ai didi

带有进度条和 GridView 的 Android 图像下载器(Android 运行时)

转载 作者:行者123 更新时间:2023-11-30 03:56:33 27 4
gpt4 key购买 nike

我是 Java 和 Android 的新手。

在此应用程序中,我正在尝试下载带有进度条的多张图像并在 GridView 中显示图像。我没有收到任何错误但出现了一些异常。当我在 eclipse 中运行这段代码,模拟器只显示“不幸停止”。那么请谁能帮我解决这个问题??

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.kailash.imagedownloader/com.kailash.imagedownloader.MainActivity}: java.lang.InstantiationException: can't instantiate class com.kailash.imagedownloader.MainActivity; no empty constructor

newInstance failed: no ()

threadid=1: thread exiting with uncaught exception (group=0x40a13300)

代码如下:

package com.kailash.imagedownloader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;

public class MainActivity extends Activity{

Button btnShowProgress;
private ProgressDialog pDialog;
GridView grid_view;
public static final int progress_bar_type = 0;

protected static final String[] URLS = {
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2944.jpg",
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_2989.jpg",
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3005.jpg",
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3012.jpg",
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3034.jpg",
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3047.jpg",
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3092.jpg",
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3110.jpg",
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3113.jpg",
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3128.jpg",
"http://cdn.cs76.net/2011/spring/lectures/6/imgs/img_3160.jpg",
};
public MainActivity(MainActivity mainActivity) {
// TODO Auto-generated constructor stub
}

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

GridView gridview = (GridView) findViewById(R.id.grid_view);
gridview.setAdapter((ListAdapter) new MainActivity(this));

btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
grid_view = (GridView) findViewById(R.id.grid_view);
btnShowProgress.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

new DownloadImages().execute(URLS);
}
});

//new DownloadImages().execute();
}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(1000);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}



public class DownloadImages extends AsyncTask {

@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}



protected Object doInBackground(Object... params) {
System.out.println("External Storage State = " + Environment.getExternalStorageState());
File directory=new File(Environment.getExternalStorageDirectory(), "/Images");
if (directory.exists()==false)
{
directory.mkdir();
}
for(int i = 0; i <URLS.length; i++) {
try {
File firstFile=new File(directory+"/" +i+ ".jpeg");
if(firstFile.exists()==false)
{
HttpClient httpClient =new DefaultHttpClient();
HttpGet httpGet =new HttpGet(URLS[i]);
HttpResponse resp = httpClient.execute(httpGet);
System.out.println("Status Code = " +resp.getStatusLine().getStatusCode());
if(resp.getStatusLine().getStatusCode()==200)
{
HttpEntity entity = resp.getEntity();
InputStream is = entity.getContent();
Boolean status = firstFile.createNewFile();

FileOutputStream foutS = new FileOutputStream(firstFile);
byte[] buffer = new byte[1024];
long total = 0;
int count;
while((count = is.read(buffer)) != -1){
total += count;
foutS.write(buffer, 0, count);
}
foutS.close();
is.close();
publishProgress(i);

}
}

}catch(MalformedURLException e){
e.printStackTrace();
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
return null;
}

protected void onProgressUpdate(String... progress) {

pDialog.setProgress(Integer.parseInt(progress[0]));
}

@SuppressWarnings("unchecked")
protected void onProgressUpdate(Object... values){
super.onProgressUpdate(values);

}
protected void onPostExecute(String file_url) {
dismissDialog(progress_bar_type);
String imagePath = Environment.getExternalStorageDirectory().toString() + "/Images";
grid_view.setBackgroundDrawable(Drawable.createFromPath(imagePath));
}
}


}

谢谢 *请帮忙*

最佳答案

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.kailash.imagedownloader/com.kailash.imagedownloader.MainActivity}: java.lang.InstantiationException: can't instantiate class com.kailash.imagedownloader.MainActivity; no empty constructor

扩展 Activity 时不应创建构造函数。当系统调用空构造函数时,创建另一个构造函数会导致该类没有空构造函数。

也没有必要创建一个,因为您根本不应该自己实例化一个 Activity 。由于某种原因,您在 onCreate 中实例化了您的 Activity 并将其转换为 ListAdapter,这是一个错误:

gridview.setAdapter((ListAdapter) new MainActivity(this));

您应该为此使用扩展 ListAdapter 的不同类。

关于带有进度条和 GridView 的 Android 图像下载器(Android 运行时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13246246/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com