为了账号安全,请及时绑定邮箱和手机立即绑定

有人可以给我一份代码或者帮我查下哪里错了吗?我已经看了一下午,但是还是没有找到哪里错了

ImageText.java
package com.example.myasynctask;

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

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ImageText extends Activity {
	private ImageView mImageView;
	private ProgressBar mprogressBar;
	private static String URL = "http://img.my.csdn.net/uploads/201504/12/1428806103_9476.png";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image);
		mImageView = (ImageView) findViewById(R.id.image);
		mprogressBar = (ProgressBar) findViewById(R.id.progressbars);
		Toast.makeText(this, "呵呵", 1).show();
		new MyAsyncTask().execute(URL);
	}

	class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mprogressBar.setVisibility(View.VISIBLE);

		}

		@Override
		protected void onPostExecute(Bitmap bitmap) {
			super.onPostExecute(bitmap);
			mprogressBar.setVisibility(View.GONE);
			mImageView.setImageBitmap(bitmap);
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			String url=params[0];
			Bitmap bitmap=null;
			URLConnection connection;
			InputStream is;
			try {
				connection=new URL(url).openConnection();
				is=connection.getInputStream();
				BufferedInputStream bis=new BufferedInputStream(is);
				Thread.sleep(3000);
				bitmap=BitmapFactory.decodeStream(bis);
				is.close();
				bis.close();			
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return bitmap;
		}
	}

}
image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ProgressBar
        android:id="@+id/progressbars"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone" />

</RelativeLayout>
不知道哪里错了,我toast在new MyAsyncTask().execute(URL);就不打印了


正在回答

6 回答

留个邮箱我发给你

0 回复 有任何疑惑可以回复我~

好的 谢谢你啊


0 回复 有任何疑惑可以回复我~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ImageTest"
        android:onClick="loadImage"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="progressTest"
        android:onClick="loadProgress"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <ProgressBar
        android:id="@+id/pg"
        android:visibility="gone"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
   
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.linhao.demo1" >
    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ImageTest"></activity>
        <activity android:name=".ProgressBarTest"></activity>
    </application>
</manifest>


1 回复 有任何疑惑可以回复我~
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.net.URL;
/**
 * Created  on 15/11/23.
 */
public class ImageTest extends Activity {
    private ImageView mImageView;
    private ProgressBar mProgressBar;
    private static String URL = "http://img.my.csdn.net/uploads/201504/12/1428806103_9476.png";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image);
        mImageView = (ImageView) findViewById(R.id.img);
        mProgressBar = (ProgressBar) findViewById(R.id.pg);
        new MyAsyncTask().execute(URL);
    }
    class MyAsyncTask extends AsyncTask<String,Void,Bitmap>
    {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressBar.setVisibility(View.VISIBLE);//显示进度条
        }
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            mProgressBar.setVisibility(View.GONE);
            mImageView.setImageBitmap(bitmap);
            //设置传递进去的参数
        }
        @Override
        protected Bitmap doInBackground(String... strings) {
            String url = strings[0];
            Bitmap bitmap = null;
            URLConnection connection;
            InputStream is;
            try {
                connection = new URL(url).openConnection();//获取网络连接对象
                is = connection.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                Thread.sleep(3000);
                bitmap = BitmapFactory.decodeStream(bis);
                //BitmapFactory -> Creates Bitmap objects from various sources, including files, streams, and byte-arrays.
                is.close();
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return bitmap;
        }
    }
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        MyAsyncTask task = new MyAsyncTask();
        task.execute();
    }
    public void loadImage(View v)
    {
        startActivity(new Intent(this, ImageTest.class));
    }
    public void loadProgress(View v)
    {
        startActivity(new Intent(this,ProgressBarTest.class));
    }
}
import android.os.AsyncTask;
import android.util.Log;
/**
 * Created  on 15/11/23.
 */
public class MyAsyncTask extends AsyncTask<Void,Void,Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        Log.d("xys", "doInBackground");
        publishProgress();
        return null;
    }
    @Override
    protected void onPreExecute() {
        Log.d("xys","onPreExecute");
        super.onPreExecute();
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        Log.d("xys","onPostExecute");
        super.onPostExecute(aVoid);
    }
    @Override
    protected void onProgressUpdate(Void... values) {
        Log.d("xys","onProgressUpdate");
        super.onProgressUpdate(values);
    }
}
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ProgressBar;
/**
 * Created on 15/11/23.
 */
public class ProgressBarTest extends Activity {
    private ProgressBar mProgressBar;
    private MyAsyncTask mTask;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progressbar);
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
        mTask = new MyAsyncTask();
        mTask.execute();
    }
    @Override
    protected void onPause() {
        super.onPause();
        if (mTask!=null && mTask.getStatus()==AsyncTask.Status.RUNNING)
        {
           mTask.cancel(true);
        }
    }
    class MyAsyncTask extends AsyncTask<Void,Integer,Void>
    {
        @Override
        protected Void doInBackground(Void... params) {
            for (int i=0;i<100;i++)
            {
                if (isCancelled())
                {
                    break;
                }
                publishProgress(i);
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            if (isCancelled())
            {
                return;
            }
            mProgressBar.setProgress(values[0]);
        }
    }
}


0 回复 有任何疑惑可以回复我~

你方便就在这里吧代码发一下吗?跪谢

0 回复 有任何疑惑可以回复我~

首先感谢你发了啊,但是我打开还报错 是什么情况啊?而且乱起八糟的,实在是惨不忍睹啊

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android必学-AsyncTask基础
  • 参与学习       40904    人
  • 解答问题       99    个

了解Android中AsyncTask的使用方法,掌握异步线程的使用

进入课程

有人可以给我一份代码或者帮我查下哪里错了吗?我已经看了一下午,但是还是没有找到哪里错了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信