2

ボタンが押されたときに画像を作成し、そのコンテンツを URL からロードしたいと考えていLinearLayoutます。Button

それを行うために作成しましたAsyncTaskが、ボタンを押すとプログラムがクラッシュします。

これが私のコードです:

package com.example.image;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    Button testButton;
    LinearLayout layout;

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

        testButton = (Button) findViewById(R.id.button1);
        layout = (LinearLayout) findViewById(R.id.LinearLayout1);

        testButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                new GetData().execute();
            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    class GetData extends AsyncTask<String, Void, String> {

        Bitmap bitmap;
        ImageView image = new ImageView(MainActivity.this);

        @Override
        protected void onPreExecute() {
        };

        @Override
        protected String doInBackground(String... params) {
            try {
                URL url = new URL("http://www.android.com/images/logo.png");
                Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            image.setImageBitmap(bitmap);
            layout.addView(image);
        }
    }

}

ここにlogcatがあります:

06-02 12:58:57.880: E/Trace(1572): error opening trace file: No such file or directory (2)
06-02 12:58:58.580: D/gralloc_goldfish(1572): Emulator without GPU emulation detected.
06-02 12:59:02.331: W/dalvikvm(1572): threadid=11: thread exiting with uncaught exception (group=0x40a13300)
06-02 12:59:02.401: E/AndroidRuntime(1572): FATAL EXCEPTION: AsyncTask #1
06-02 12:59:02.401: E/AndroidRuntime(1572): java.lang.RuntimeException: An error occured while executing doInBackground()
06-02 12:59:02.401: E/AndroidRuntime(1572):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.lang.Thread.run(Thread.java:856)
06-02 12:59:02.401: E/AndroidRuntime(1572): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at com.example.image.MainActivity$GetData.doInBackground(MainActivity.java:63)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at com.example.image.MainActivity$GetData.doInBackground(MainActivity.java:1)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-02 12:59:02.401: E/AndroidRuntime(1572):     ... 5 more
06-02 12:59:02.401: E/AndroidRuntime(1572): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.io.Posix.getaddrinfo(Native Method)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
06-02 12:59:02.401: E/AndroidRuntime(1572):     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
06-02 12:59:02.401: E/AndroidRuntime(1572):     ... 22 more
06-02 12:59:02.401: E/AndroidRuntime(1572): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
06-02 12:59:02.401: E/AndroidRuntime(1572):     ... 25 more
4

3 に答える 3

2

ログのように:

SecurityException: 許可が拒否されました (INTERNET 許可がありませんか?)

INTERNET 権限を追加するのを忘れていることを意味しますAndroidManifest。次のように追加します。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
于 2013-06-02T13:05:41.993 に答える
0

このようにAndroidマニフェストに権限を追加してみてください。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
于 2013-06-02T13:06:28.353 に答える
0

ビットマップを削除

そのためのグローバル変数を作成しました

     Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

これだけ書いて

     bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
于 2013-06-02T13:12:39.927 に答える