我的应用程序适用于 Gingerbread...ICS 和 HC 崩溃

发布于 2024-12-26 20:43:20 字数 18571 浏览 1 评论 0原文

我需要你的帮助。 我的应用程序在 GingerBread 上运行良好,但在 ICS 和 HC 上崩溃。 这是logcat

01-14 20:26:51.831: E/AndroidRuntime(698): FATAL EXCEPTION: main
01-14 20:26:51.831: E/AndroidRuntime(698): android.os.NetworkOnMainThreadException
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-14 20:26:51.831: E/AndroidRuntime(698):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-14 20:26:51.831: E/AndroidRuntime(698):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-14 20:26:51.831: E/AndroidRuntime(698):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-14 20:26:51.831: E/AndroidRuntime(698):  at com.bozz.milaircraft.f23$ImageAdapter.getView(f23.java:125)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-14 20:26:51.831: E/AndroidRuntime(698):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.os.Looper.loop(Looper.java:137)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-14 20:26:51.831: E/AndroidRuntime(698):  at java.lang.reflect.Method.invokeNative(Native Method)
01-14 20:26:51.831: E/AndroidRuntime(698):  at java.lang.reflect.Method.invoke(Method.java:511)
01-14 20:26:51.831: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-14 20:26:51.831: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-14 20:26:51.831: E/AndroidRuntime(698):  at dalvik.system.NativeStart.main(Native Method)

这是layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <Button
            android:id="@+id/homebutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/homebutton" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/nomeF22Raptor"
            android:textColor="#FFD800"
            android:textSize="22dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/searchbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/searchbutton" >
        </Button>
    </LinearLayout>

    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="420dp"
        android:layout_weight="0.80"
        android:background="@drawable/background" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ScrollView
                        android:id="@+id/ScrollView01"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <LinearLayout
                            android:id="@+id/linearLayout2"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/datiF22Raptor" />
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ScrollView
                        android:id="@+id/ScrollView01"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <LinearLayout
                            android:id="@+id/linearLayout2"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/cartecF22Raptor" />
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ScrollView
                        android:id="@+id/ScrollView01"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <LinearLayout
                            android:id="@+id/linearLayout2"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/utilizzatoriF22Raptor" />
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <Gallery
                        android:id="@+id/gallery"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"/>

                    <ImageView
                        android:id="@+id/ImageView01"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center" >
                    </ImageView>
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

这是java 代码

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class f23 extends Activity {



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

        Button bhome = (Button) findViewById(R.id.homebutton);
        bhome.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.bozz.milaircraft.MENU"));
            }
        });

        TabHost th = (TabHost) findViewById(R.id.tabhost);
        th.setup();
        TabSpec specs = th.newTabSpec("tag1");
        specs.setContent(R.id.tab1);
        specs.setIndicator("BRIEFING");
        th.addTab(specs);
        specs = th.newTabSpec("tag2");
        specs.setContent(R.id.tab2);
        specs.setIndicator("DESIGN");
        th.addTab(specs);
        specs = th.newTabSpec("tag3");
        specs.setContent(R.id.tab3);
        specs.setIndicator("USERS");
        th.addTab(specs);
        specs = th.newTabSpec("tag4");
        specs.setContent(R.id.tab4);
        specs.setIndicator("GALLERY");
        th.addTab(specs);


        Gallery ga = (Gallery) findViewById(R.id.gallery);
        ga.setAdapter(new ImageAdapter(this));



    }


    public class ImageAdapter extends BaseAdapter {
        /** The parent context */
        private Context myContext;
        int imageBackground;

        /** URL-Strings to some remote images. */
        private String[] myRemoteImages = {
                "http://www.medicinarozzano.it/images/milair/f4_1.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_2.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_3.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_4.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_5.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_6.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_7.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_8.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_9.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_10.jpg"};

        /** Simple Constructor saving the 'parent' context. */
        public ImageAdapter(Context c) {
            this.myContext = c;

        }

        /** Returns the amount of images we have defined. */
        public int getCount() {
            return this.myRemoteImages.length;
        }

        /* Use the array-Positions as unique IDs */
        public Object getItem(int position) {
            return position;
        }

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

        /**
         * Returns a new ImageView to be displayed, depending on the position
         * passed.
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(this.myContext);

            try {
                /* Open a new URL and get the InputStream to load data from it. */
                URL aURL = new URL(myRemoteImages[position]);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                /* Buffered is always good for a performance plus. */
                BufferedInputStream bis = new BufferedInputStream(is);
                /* Decode url-data to a bitmap. */
                Bitmap bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();
                /* Apply the Bitmap to the ImageView that will be returned. */
                i.setImageBitmap(bm);
            } catch (IOException e) {
                i.setImageResource(R.drawable.f4_1);
                Log.e("DEBUGTAG", "Remote Image Exception", e);
            }

            /* Image should be scaled as width/height are set. */
            i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            /* Set the Width/Height of the ImageView. */
            i.setLayoutParams(new Gallery.LayoutParams(480, 640));
            return i;
        }

        /**
         * Returns the size (0.0f to 1.0f) of the views depending on the
         * 'offset' to the center.
         */
        public float getScale(boolean focused, int offset) {
            /* Formula: 1 / (2 ^ offset) */
            return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
        }
    }
}

当我单击“图库”选项卡时应用程序崩溃。

I need your help.
My App works fine on GingerBread, but on ICS and HC crash.
This is the logcat

01-14 20:26:51.831: E/AndroidRuntime(698): FATAL EXCEPTION: main
01-14 20:26:51.831: E/AndroidRuntime(698): android.os.NetworkOnMainThreadException
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-14 20:26:51.831: E/AndroidRuntime(698):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-14 20:26:51.831: E/AndroidRuntime(698):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-14 20:26:51.831: E/AndroidRuntime(698):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-14 20:26:51.831: E/AndroidRuntime(698):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-14 20:26:51.831: E/AndroidRuntime(698):  at com.bozz.milaircraft.f23$ImageAdapter.getView(f23.java:125)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-14 20:26:51.831: E/AndroidRuntime(698):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.View.measure(View.java:12723)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.os.Looper.loop(Looper.java:137)
01-14 20:26:51.831: E/AndroidRuntime(698):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-14 20:26:51.831: E/AndroidRuntime(698):  at java.lang.reflect.Method.invokeNative(Native Method)
01-14 20:26:51.831: E/AndroidRuntime(698):  at java.lang.reflect.Method.invoke(Method.java:511)
01-14 20:26:51.831: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-14 20:26:51.831: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-14 20:26:51.831: E/AndroidRuntime(698):  at dalvik.system.NativeStart.main(Native Method)

Here is the layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <Button
            android:id="@+id/homebutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/homebutton" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/nomeF22Raptor"
            android:textColor="#FFD800"
            android:textSize="22dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/searchbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/searchbutton" >
        </Button>
    </LinearLayout>

    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="420dp"
        android:layout_weight="0.80"
        android:background="@drawable/background" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ScrollView
                        android:id="@+id/ScrollView01"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <LinearLayout
                            android:id="@+id/linearLayout2"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/datiF22Raptor" />
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ScrollView
                        android:id="@+id/ScrollView01"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <LinearLayout
                            android:id="@+id/linearLayout2"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/cartecF22Raptor" />
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ScrollView
                        android:id="@+id/ScrollView01"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <LinearLayout
                            android:id="@+id/linearLayout2"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/utilizzatoriF22Raptor" />
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >

                    <Gallery
                        android:id="@+id/gallery"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"/>

                    <ImageView
                        android:id="@+id/ImageView01"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center" >
                    </ImageView>
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

and this is the java code

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class f23 extends Activity {



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

        Button bhome = (Button) findViewById(R.id.homebutton);
        bhome.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.bozz.milaircraft.MENU"));
            }
        });

        TabHost th = (TabHost) findViewById(R.id.tabhost);
        th.setup();
        TabSpec specs = th.newTabSpec("tag1");
        specs.setContent(R.id.tab1);
        specs.setIndicator("BRIEFING");
        th.addTab(specs);
        specs = th.newTabSpec("tag2");
        specs.setContent(R.id.tab2);
        specs.setIndicator("DESIGN");
        th.addTab(specs);
        specs = th.newTabSpec("tag3");
        specs.setContent(R.id.tab3);
        specs.setIndicator("USERS");
        th.addTab(specs);
        specs = th.newTabSpec("tag4");
        specs.setContent(R.id.tab4);
        specs.setIndicator("GALLERY");
        th.addTab(specs);


        Gallery ga = (Gallery) findViewById(R.id.gallery);
        ga.setAdapter(new ImageAdapter(this));



    }


    public class ImageAdapter extends BaseAdapter {
        /** The parent context */
        private Context myContext;
        int imageBackground;

        /** URL-Strings to some remote images. */
        private String[] myRemoteImages = {
                "http://www.medicinarozzano.it/images/milair/f4_1.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_2.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_3.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_4.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_5.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_6.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_7.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_8.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_9.jpg",
                "http://www.medicinarozzano.it/images/milair/f4_10.jpg"};

        /** Simple Constructor saving the 'parent' context. */
        public ImageAdapter(Context c) {
            this.myContext = c;

        }

        /** Returns the amount of images we have defined. */
        public int getCount() {
            return this.myRemoteImages.length;
        }

        /* Use the array-Positions as unique IDs */
        public Object getItem(int position) {
            return position;
        }

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

        /**
         * Returns a new ImageView to be displayed, depending on the position
         * passed.
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(this.myContext);

            try {
                /* Open a new URL and get the InputStream to load data from it. */
                URL aURL = new URL(myRemoteImages[position]);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                /* Buffered is always good for a performance plus. */
                BufferedInputStream bis = new BufferedInputStream(is);
                /* Decode url-data to a bitmap. */
                Bitmap bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();
                /* Apply the Bitmap to the ImageView that will be returned. */
                i.setImageBitmap(bm);
            } catch (IOException e) {
                i.setImageResource(R.drawable.f4_1);
                Log.e("DEBUGTAG", "Remote Image Exception", e);
            }

            /* Image should be scaled as width/height are set. */
            i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            /* Set the Width/Height of the ImageView. */
            i.setLayoutParams(new Gallery.LayoutParams(480, 640));
            return i;
        }

        /**
         * Returns the size (0.0f to 1.0f) of the views depending on the
         * 'offset' to the center.
         */
        public float getScale(boolean focused, int offset) {
            /* Formula: 1 / (2 ^ offset) */
            return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
        }
    }
}

The App crash when I click on the Gallery Tab.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

并安 2025-01-02 20:43:20

在应用程序的主线程上执行(可能阻塞)网络操作是一个坏主意,而这正是您正在做的事情。在 Honeycomb(Android 3.0)之前,这只是一个坏主意。从 Android 3.0 开始,这是被禁止的,如果您尝试这样做,您会收到此异常。

请参阅http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

您需要将此网络逻辑外包给单独的异步线程。或者,查看Handler。您将需要更改代码架构;没有简单的解决方法。

It is a bad idea to perform (potentially blocking) network operations on your app's main thread, which is what you are doing. Before Honeycomb (Android 3.0), it was just a bad idea. From Android 3.0 onwards, it is prohibited, and you receive this exception if you try to do so.

See http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

You need to farm out this network logic to a separate asynchronous thread. Or, look at a Handler. You're going to need to change your code architecture; no easy fix.

源来凯始玺欢你 2025-01-02 20:43:20

在 Honeycomb 网络中,UI 线程上不允许使用。原因是它会减慢用户界面 (UI) 的速度。

相反,尝试在后台线程中下载图像,实现此目的的最简单方法是使用 异步任务

In Honeycomb networking is not allowed on the UI thread. The reason is that it slows down the user interface (UI).

Instead try to download your images in an background thread, the easiest way to implement this is using an AsyncTask.

メ斷腸人バ 2025-01-02 20:43:20

使用它需要您自担风险:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll()
.build();
StrictMode.setThreadPolicy(policy);

资源:
http://developer.android.com/reference/android/os/StrictMode.ThreadPolicy.Builder.html

Use it at your own risk:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll()
.build();
StrictMode.setThreadPolicy(policy);

Resource:
http://developer.android.com/reference/android/os/StrictMode.ThreadPolicy.Builder.html

似梦非梦 2025-01-02 20:43:20

您可以查看操作系统的版本,并根据它执行代码。

例如:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)

You can see the version of the OS, and execute a code depending of it.

For example:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
变身佩奇 2025-01-02 20:43:20

这就是我解决问题的方法:

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.Intent;
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.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.TabSpec;

public class f24 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.f24);

        Button bhome = (Button) findViewById(R.id.homebutton);
        bhome.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent ("com.bozz.milaircraft.MENU"));
            }
        });


        TabHost th = (TabHost) findViewById(R.id.tabhost);
        th.setup();
        TabSpec specs = th.newTabSpec("tag1");
        specs.setContent(R.id.tab1);
        specs.setIndicator("BRIEFING");
        th.addTab(specs);
        specs = th.newTabSpec("tag2");
        specs.setContent(R.id.tab2);
        specs.setIndicator("DESIGN");
        th.addTab(specs);
        specs = th.newTabSpec("tag3");
        specs.setContent(R.id.tab3);
        specs.setIndicator("USERS");
        th.addTab(specs);
        specs = th.newTabSpec("tag4");
        specs.setContent(R.id.tab4);
        specs.setIndicator("GALLERY");
        th.addTab(specs);

        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://www.medicinarozzano.it/images/milair/f4_1.jpg");
        PhotoURLS.add("http://www.medicinarozzano.it/images/milair/f4_1.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.Gallery1);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_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(150, 120));
            i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            i.setBackgroundResource(mGalleryItemBackground);

            return i;
        }
    }

}

但现在我有另一个问题:

如果我不在 Android Manifest 上声明任何 SDK,则该应用程序在姜饼和 HC 以及 ICS 上都能完美运行。但是,如果我声明最小 SDK 大于 1 或 TargetSDK,则该应用程序在姜饼上运行良好,但在 HC 和 ISC 上则不行。当我单击图库中的图像时,图像查看器上没有显示任何内容。然而,弹出一个 toast 错误。

此外,如果没有互联网连接,应用程序就会崩溃。

此致
蒂贝里奥·博佐蒂

编辑:
我可以删除 SDK,但在蜂窝上尚不清楚。这意味着布局不兼容。

This is how I solve the problem:

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.Intent;
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.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.TabSpec;

public class f24 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.f24);

        Button bhome = (Button) findViewById(R.id.homebutton);
        bhome.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent ("com.bozz.milaircraft.MENU"));
            }
        });


        TabHost th = (TabHost) findViewById(R.id.tabhost);
        th.setup();
        TabSpec specs = th.newTabSpec("tag1");
        specs.setContent(R.id.tab1);
        specs.setIndicator("BRIEFING");
        th.addTab(specs);
        specs = th.newTabSpec("tag2");
        specs.setContent(R.id.tab2);
        specs.setIndicator("DESIGN");
        th.addTab(specs);
        specs = th.newTabSpec("tag3");
        specs.setContent(R.id.tab3);
        specs.setIndicator("USERS");
        th.addTab(specs);
        specs = th.newTabSpec("tag4");
        specs.setContent(R.id.tab4);
        specs.setIndicator("GALLERY");
        th.addTab(specs);

        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://www.medicinarozzano.it/images/milair/f4_1.jpg");
        PhotoURLS.add("http://www.medicinarozzano.it/images/milair/f4_1.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.Gallery1);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_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(150, 120));
            i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            i.setBackgroundResource(mGalleryItemBackground);

            return i;
        }
    }

}

But now I have another problem:

If i don not declare any SDK on Android Manifest, the app works perfectly on both gingerbread and HC, and ICS. However, if i declare a minimum SDK greater than 1 or TargetSDK, the app works fine on gingerbread but not on HC and ISC. When i click on an image in the gallery nothing appears on the image viewer. Yet, a toast error pops up.

Also, if there is no internet connection the app crashes.

Best regards
Tiberio Bozotti

EDIT:
i could remove the SDK but on honeycomb it is not clear. Meaning that the layout is not compatible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文