0

嗨,如果我运行我的项目,它会指示此 logcat 错误...如何解决此错误。请帮我...

日志猫:`

04-28 15:19:01.210: DEBUG/AndroidRuntime(376): >>>>>>>>>>>>>> AndroidRuntime START     
<<<<<<<<<<<<<<
04-28 15:19:01.210: DEBUG/AndroidRuntime(376): CheckJNI is ON
04-28 15:19:01.619: DEBUG/AndroidRuntime(376): --- registering native functions ---
04-28 15:19:02.170: INFO/ActivityManager(60): Process com.android.email (pid 228) has 
died.
04-28 15:19:03.340: DEBUG/dalvikvm(178): GC_EXPLICIT freed 78 objects / 8216 bytes in 
111ms
04-28 15:19:03.439: DEBUG/PackageParser(60): Scanning package: /data/app/vmdl15137.tmp
04-28 15:19:03.469: INFO/PackageParser(60): com.isummation.igallery: compat added 
android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_PHONE_STATE
04-28 15:19:03.669: INFO/PackageManager(60): Removing non-system   
package:com.isummation.igallery
04-28 15:19:03.669: INFO/ActivityManager(60): Force stopping package 
com.isummation.igallery uid=10062
04-28 15:19:03.689: INFO/Process(60): Sending signal. PID: 363 SIG: 9
04-28 15:19:03.779: WARN/InputManagerService(60): Window already focused, ignoring 
focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43f2ac48
04-28 15:19:04.599: DEBUG/PackageManager(60): Scanning package com.isummation.igallery
04-28 15:19:04.609: INFO/PackageManager(60): Package com.isummation.igallery codePath 
changed from /data/app/com.isummation.igallery-1.apk to /data/app
/com.isummation.igallery-2.apk; Retaining data and using new
04-28 15:19:04.629: INFO/PackageManager(60): /data/app/com.isummation.igallery-2.apk 
changed; unpacking
04-28 15:19:04.670: DEBUG/installd(35): DexInv: --- BEGIN '/data
/app/com.isummation.igallery-2.apk' ---
04-28 15:19:05.240: DEBUG/dalvikvm(383): DexOpt: load 67ms, verify 154ms, opt 4ms
04-28 15:19:05.260: DEBUG/installd(35): DexInv: --- END '/data
/app/com.isummation.igallery-2.apk' (success) ---
04-28 15:19:05.260: WARN/PackageManager(60): Code path for pkg : 
com.isummation.igallery changing from /data/app/com.isummation.igallery-1.apk to 
/data/app/com.isummation.igallery-2.apk
04-28 15:19:05.270: WARN/PackageManager(60): Resource path for pkg :      
com.isummation.igallery changing from /data/app/com.isummation.igallery-1.apk to    
/data/app/com.isummation.igallery-2.apk
04-28 15:19:05.270: DEBUG/PackageManager(60):   Activities:  
com.isummation.igallery.MyGallery
04-28 15:19:05.280: INFO/ActivityManager(60): Force stopping package 
com.isummation.igallery uid=10062
04-28 15:19:05.839: DEBUG/dalvikvm(60): GC_FOR_MALLOC freed 7590 objects / 469992 
bytes in 202ms
04-28 15:19:06.040: INFO/installd(35): move /data/dalvik-
cache/data@app@com.isummation.igallery-2.apk@classes.dex -> /data/dalvik-  
cache/data@app@com.isummation.igallery-2.apk@classes.dex
04-28 15:19:06.050: DEBUG/PackageManager(60): New package installed in /data/app
/com.isummation.igallery-2.apk
04-28 15:19:06.569: INFO/ActivityManager(60): Force stopping package 
com.isummation.igallery uid=10062
04-28 15:19:06.769: DEBUG/dalvikvm(127): GC_EXPLICIT freed 1464 objects / 82560 bytes 
in 154ms
04-28 15:19:07.489: DEBUG/dalvikvm(60): GC_EXPLICIT freed 5017 objects / 301832 
bytes  in 253ms
04-28 15:19:07.499: INFO/installd(35): unlink /data/dalvik-  
cache/data@app@com.isummation.igallery-1.apk@classes.dex
04-28 15:19:07.679: DEBUG/AndroidRuntime(376): Shutting down VM
04-28 15:19:07.699: DEBUG/dalvikvm(376): Debugger has detached; object registry had 1 
entries
04-28 15:19:07.759: INFO/AndroidRuntime(376): NOTE: attach of thread 'Binder Thread 
#3' failed
04-28 15:19:07.809: WARN/RecognitionManagerService(60): no available voice 
recognition services found
04-28 15:19:09.150: DEBUG/AndroidRuntime(389): >>>>>>>>>>>>>> AndroidRuntime START 
<<<<<<<<<<<<<<
04-28 15:19:09.150: DEBUG/AndroidRuntime(389): CheckJNI is ON
04-28 15:19:09.529: DEBUG/AndroidRuntime(389): --- registering native functions ---
04-28 15:19:10.929: INFO/ActivityManager(60): Starting activity: Intent { 
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 
cmp=com.isummation.igallery/.MyGallery }
04-28 15:19:11.050: DEBUG/AndroidRuntime(389): Shutting down VM
04-28 15:19:11.080: DEBUG/dalvikvm(389): Debugger has detached; object registry had 1 
entries
04-28 15:19:11.170: INFO/AndroidRuntime(389): NOTE: attach of thread 'Binder Thread 
#3' failed
04-28 15:19:11.280: INFO/ActivityManager(60): Start proc com.isummation.igallery for   
activity com.isummation.igallery/.MyGallery: pid=396 uid=10062 gids={3003, 1015}
04-28 15:19:11.909: DEBUG/dalvikvm(33): GC_EXPLICIT freed 277 objects / 10608 bytes in 
594ms
04-28 15:19:12.290: DEBUG/dalvikvm(33): GC_EXPLICIT freed 37 objects / 1600 bytes in 
303ms
04-28 15:19:12.759: DEBUG/dalvikvm(33): GC_EXPLICIT freed 2 objects / 64 bytes in 469ms
04-28 15:19:13.560: INFO/ActivityManager(60): Displayed activity    
com.isummation.igallery/.MyGallery: 2543 ms (total 2543 ms)
04-28 15:19:14.040: WARN/dalvikvm(396): threadid=7: thread exiting with uncaught 
exception (group=0x4001d800)
04-28 15:19:14.080: ERROR/AndroidRuntime(396): FATAL EXCEPTION: AsyncTask #1
04-28 15:19:14.080: ERROR/AndroidRuntime(396): java.lang.RuntimeException: An error 
occured while executing doInBackground()
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at   
android.os.AsyncTask$3.done(AsyncTask.java:200)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
java.lang.Thread.run(Thread.java:1096)
04-28 15:19:14.080: ERROR/AndroidRuntime(396): Caused by: java.lang.RuntimeException:  
Can't create handler inside thread that has not called Looper.prepare()
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at android.os.Handler.<init>
(Handler.java:121)
 04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at android.widget.Toast.<init>
(Toast.java:68)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
android.widget.Toast.makeText(Toast.java:231)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
com.isummation.igallery.MyGallery.LoadThumbnailFromURL(MyGallery.java:115)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
com.isummation.igallery.MyGallery.access$2(MyGallery.java:93)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
com.isummation.igallery.MyGallery$AddImageTask.doInBackground(MyGallery.java:75)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at    
com.isummation.igallery.MyGallery$AddImageTask.doInBackground(MyGallery.java:1)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
android.os.AsyncTask$2.call(AsyncTask.java:185)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     at 
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-28 15:19:14.080: ERROR/AndroidRuntime(396):     ... 4 more
04-28 15:19:14.110: WARN/ActivityManager(60):   Force finishing activity 
com.isummation.igallery/.MyGallery
04-28 15:19:20.090: DEBUG/dalvikvm(178): GC_EXPLICIT freed 167 objects / 11912 bytes 
 in 130ms
 04-28 15:19:25.110: DEBUG/dalvikvm(262): GC_EXPLICIT freed 29 objects / 1488 bytes 
 in 143ms

我的编码:

package com.isummation.igallery;

 import java.io.InputStream;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.ArrayList;

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

    import android.app.Activity;
    import android.content.Context;
    import android.content.res.TypedArray;  
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    import android.widget.Toast;

    public class MyGallery extends Activity {

    private ImageAdapter imageAdapter;

    private ArrayList<String> PhotoURLS = new ArrayList<String>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imageAdapter = new ImageAdapter(this);
    final ImageView imgView = (ImageView) findViewById(R.id.GalleryView);
    Gallery g = (Gallery) findViewById(R.id.Gallery);
    g.setAdapter(imageAdapter);
    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            imgView.setImageDrawable(LoadImageFromURL(PhotoURLS
                    .get(position)));
            imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        }
    });

    // replace this code to set your image urls in list
    PhotoURLS.add("http://10.0.2.2/upload/3BC52DAA-AFD8-50B5-DA5096B82422275F.jpg");
    PhotoURLS.add("http://10.0.2.2/upload/B652F78D-C760-B674-7A6E61E505A05A0F.jpg");

    new AddImageTask().execute();

     }

    class AddImageTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... unused) {
        for (String url : PhotoURLS) {
            String filename = url.substring(url.lastIndexOf("/") + 1,
                    url.length());
            filename = "th_" + filename;
            String thumburl = url.substring(0, url.lastIndexOf("/") + 1);
            imageAdapter.addItem(LoadThumbnailFromURL(thumburl + filename));
            publishProgress();
            //SystemClock.sleep(200);
        }

        return (null);
    }

    @Override
    protected void onProgressUpdate(Void... unused) {
        imageAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onPostExecute(Void unused) {
    }
    }

     private Drawable LoadThumbnailFromURL(String url) {
       try {
        URLConnection connection = new URL(url).openConnection();
        String contentType = connection.getHeaderField("Content-Type");
        boolean isImage = contentType.startsWith("image/");
        if(isImage){
            HttpGet httpRequest = new HttpGet(url);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient
                    .execute(httpRequest);
            HttpEntity entity = response.getEntity();
            BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);

            InputStream is = bufferedHttpEntity.getContent();
            Drawable d = Drawable.createFromStream(is, "src Name");
            return d;
        } else {
            Bitmap b = BitmapFactory.decodeResource(getResources(), 
        R.drawable.no_image);
            Drawable d = new BitmapDrawable(b);
            return d;
        }
        } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG)
                .show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
        return null;
        }
       }

      private Drawable LoadImageFromURL(String url) {
      try {
        URLConnection connection = new URL(url).openConnection();
        String contentType = connection.getHeaderField("Content-Type");
        boolean isImage = contentType.startsWith("image/");
        if(isImage){
            HttpGet httpRequest = new HttpGet(url);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient
                    .execute(httpRequest);
            HttpEntity entity = response.getEntity();
            BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(
                    entity);
            InputStream is = bufferedHttpEntity.getContent();

            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, o);

            // The new size we want to scale to
            final int REQUIRED_SIZE = 150;

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // Decode with inSampleSize
            is = bufferedHttpEntity.getContent();
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap b = BitmapFactory.decodeStream(is, null, o2);
            Drawable d = new BitmapDrawable(b);
            return d;
        } else {
            Bitmap b = BitmapFactory.decodeResource(getResources(), 
        R.drawable.no_image);
            Drawable d = new BitmapDrawable(b);
            return d;
        }
      } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG)
                .show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
        return null;
       }
      }

     public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    ArrayList<Drawable> drawablesFromUrl = new ArrayList<Drawable>();

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(R.styleable.GalleryTheme);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        a.recycle();
    }

    public void addItem(Drawable item) {
        drawablesFromUrl.add(item);
    }

    public int getCount() {
        return drawablesFromUrl.size();
    }

    public Drawable getItem(int position) {
        return drawablesFromUrl.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);

        i.setImageDrawable(drawablesFromUrl.get(position));
        i.setLayoutParams(new Gallery.LayoutParams(80, 70));
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setBackgroundResource(mGalleryItemBackground);

        return i;
    }
   }

    }
4

1 回答 1

1

您不能从 UI 线程显示 Toast(并且通常修改 UI)。在AsyncTask你有特殊的方法:(onPreExecuteonPostExecuteonProgressUpdate()

于 2011-04-28T10:12:42.743 回答