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

为什么出错了,大神帮帮我看下代码哪错了

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) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.image);

mImageView=(ImageView) findViewById(R.id.image);

mProgressBar=(ProgressBar) findViewById(R.id.progressbar);

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) {

// TODO Auto-generated method stub

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;

}

}

}

public class MainActivity extends Activity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        MyAsyncTask task=new MyAsyncTask();

        task.execute();

    }

    

    @SuppressWarnings("unused")

private void loadImage(View view){

    startActivity(new Intent(this,ImageTest.class));

    }

}

public class MyAsyncTask extends AsyncTask<Void, Void, Void>{


@Override

protected Void doInBackground(Void... params) {

// TODO Auto-generated method stub

Log.d("xys", "doInBackground");

publishProgress();

return null;

}

@Override

protected void onPreExecute() {

// TODO Auto-generated method stub

super.onPreExecute();

Log.d("xys", "onPreExecute");

}

@Override

protected void onPostExecute(Void result) {

// TODO Auto-generated method stub

super.onPostExecute(result);

Log.d("xys", "onPostExecute");

}

@Override

protected void onProgressUpdate(Void... values) {

// TODO Auto-generated method stub

super.onProgressUpdate(values);

Log.d("xys", "onProgressUpdate");

}

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="16dp">

    

    

    

    <ImageView 

        android:id="@+id/image"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        />

    <ProgressBar

        android:id="@+id/progressbar"

        android:visibility="gone"

        android:layout_centerInParent="true"

         android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />


</RelativeLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.example.dan55.MainActivity" >


    <Button

        android:onClick="loadImage"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Image Test" />


</LinearLayout>


正在回答

1 回答

找到了原来在onclick方法出错了,谢谢

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

举报

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

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

进入课程

为什么出错了,大神帮帮我看下代码哪错了

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