1

我是 Java 和 Android 的新手。

在此应用程序中,我尝试使用进度条下载多个图像并在 GridView 中显示图像。我没有收到任何错误,但有一些例外。当我在 Eclipse 中运行此代码时,模拟器只显示“不幸停止”。所以请谁能帮我解决这个问题?

java.lang.RuntimeException:无法实例化活动 ComponentInfo{com.kailash.imagedownloader/com.kailash.imagedownloader.MainActivity}:java.lang.InstantiationException:无法实例化类 com.kailash.imagedownloader.MainActivity;没有空的构造函数

新实例失败:否()

threadid=1:线程以未捕获的异常退出(组=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));
       }
 }


}

谢谢*请帮忙*

4

1 回答 1

1

java.lang.RuntimeException:无法实例化活动 ComponentInfo{com.kailash.imagedownloader/com.kailash.imagedownloader.MainActivity}:java.lang.InstantiationException:无法实例化类 com.kailash.imagedownloader.MainActivity;没有空的构造函数

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

也不需要创建一个,因为您根本不应该自己实例化一个活动。出于某种原因,您在内部实例化您的活动onCreate并将其转换为ListAdapter,这是一个错误:

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

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

于 2012-11-06T07:27:19.460 回答