Asynctask网络连接问题

时间:2014-07-17 09:09:25

标签: android android-asynctask connection

我有fourone1活动,它包括asynctask。当网络连接可用时,活动运行良好。当网络消失时,程序进入网络设置,但程序在后台崩溃。我怎么解决这个问题?

public class FourOne1 extends BaseActivityTwo {

    public static final String URL = 
            "http://fastestlaps.com/photos/bmw_m5_e60_4ebe96e6dad77.jpg";


    private String[] menutitles;
    private TypedArray menuIcons;

    TextView tv1 ;
    TextView tv2 ;
    TextView tv3 ;
    TextView tv4 ;
    TextView tv5 ;
    TextView tv6 ;

    ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


    setContentView(R.layout.four_one_1);


    if (!isNetworkAvailable()) {
        Toast.makeText(getApplicationContext(),
                "Internet Connection Required", Toast.LENGTH_SHORT).show();
        FourOne1.this.startActivity(new Intent(
                   Settings.ACTION_WIRELESS_SETTINGS));
               //finish();


    } else { 
    new GetXMLTask().execute();
    }






    menutitles = getResources().getStringArray(R.array.titlesthree); // load titles from strings.xml

    menuIcons = getResources()
                    .obtainTypedArray(R.array.iconsthree);//load icons from strings.xml



    set(menutitles, menuIcons);


    imageView = (ImageView) findViewById(R.id.imageView1);

    tv1 = (TextView)findViewById(R.id.textView4);
    tv2 = (TextView)findViewById(R.id.textView5);
    tv3 = (TextView)findViewById(R.id.textView6);
    tv4 = (TextView)findViewById(R.id.textView7);
    tv5 = (TextView)findViewById(R.id.textView8);
    tv6 = (TextView)findViewById(R.id.textView9);

    tv1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub          

            Intent intent = new Intent(getApplicationContext(), FourOne1One.class);
            startActivity(intent);

        }
    });


tv2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub          

            Intent intent = new Intent(getApplicationContext(), FourOne1Two.class);
            startActivity(intent);

        }
    });

tv3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub          

        Intent intent = new Intent(getApplicationContext(), FourOne1Three.class);
        startActivity(intent);

    }
});


tv4.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub          

        Intent intent = new Intent(getApplicationContext(), FourOne1Four.class);
        startActivity(intent);

    }
});


tv5.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub          

        Intent intent = new Intent(getApplicationContext(), FourOne1Five.class);
        startActivity(intent);

    }
});


tv6.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub          

        Intent intent = new Intent(getApplicationContext(), FourOne1Six.class);
        startActivity(intent);

    }
});


    // Create an object for subclass of AsyncTask
    GetXMLTask task = new GetXMLTask();
    // Execute the task
    task.execute(new String[] { URL });


    }


    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }





    private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {





        @Override
        protected Bitmap doInBackground(String... urls) {
            Bitmap map = null;
            for (String url : urls) {
                map = downloadImage(url);
            }
            return map;
        }

        // Sets the Bitmap returned by doInBackground
        @Override
        protected void onPostExecute(Bitmap result) {
            imageView.setImageBitmap(result);
        }

        // Creates Bitmap from InputStream and returns it
        private Bitmap downloadImage(String url) {
            Bitmap bitmap = null;
            InputStream stream = null;
            BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;

            try {
                stream = getHttpConnection(url);
                bitmap = BitmapFactory.
                        decodeStream(stream, null, bmOptions);
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return bitmap;
        }

        // Makes HttpURLConnection and returns InputStream
        private InputStream getHttpConnection(String urlString)
                throws IOException {
            InputStream stream = null;
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();

            try {
                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                httpConnection.setRequestMethod("GET");
                httpConnection.connect();

                if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    stream = httpConnection.getInputStream();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return stream;
        }
    }



}

Logcat错误

07-17 04:54:32.770: E/AndroidRuntime(1367): java.lang.RuntimeException: An error occured while executing doInBackground()
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at java.lang.Thread.run(Thread.java:841)
    07-17 04:54:32.770: E/AndroidRuntime(1367): Caused by: java.lang.NullPointerException
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at com.example.test.FourOne1$GetXMLTask.downloadImage(FourOne1.java:225)
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at com.example.test.FourOne1$GetXMLTask.doInBackground(FourOne1.java:203)
    07-17 04:54:32.770: E/AndroidRuntime(1367):     at com.example.test.FourOne1$GetXMLTask.doInBackground(FourOne1.java:1)

0 个答案:

没有答案