当我试图下载图像时,我得到空指针异常...

时间:2014-03-06 09:59:26

标签: android image download imagedownload

我想从互联网上下载图片...但我得到空指针异常 如果有任何

,我也想知道bw URL连接和HTTP连接的主要区别

请告诉我解决方案
    这是我的代码     谢谢你

public class DetailsActivity extends Activity {
private TextView textView1; 
private TextView textView2;
private TextView textView3;
private TextView textView4;
private TextView textView5;
//private ImageView image;

ImageView image;
static Bitmap bm;
ProgressDialog pd;
String imageUrl = "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png";
BitmapFactory.Options bmOptions;




public class ImageDownload extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;
        loadBitmap(imageUrl, bmOptions);
        return imageUrl;
    }

    protected void onPostExecute(String imageUrl) {
        pd.dismiss();
        if (!imageUrl.equals("")) {
            image.setImageBitmap(bm);//error here null pointer exception
        } else {
            Toast.makeText(DetailsActivity.this,
                    "Não foi possível obter resultados", Toast.LENGTH_LONG)
                    .show();
        }
    }

}

public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
        bm = BitmapFactory.decodeStream(in, null, options);
        in.close();
    } catch (IOException e1) {
    }
    return bm;
}

private static InputStream OpenHttpConnection(String strURL)
        throws IOException {
    InputStream inputStream = null;
    URL url = new URL(strURL);
    URLConnection conn = url.openConnection();

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = httpConn.getInputStream();
        }
    } catch (Exception ex) {
    }
    return inputStream;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    image = (ImageView)findViewById(R.id.imageView2);
    ActionBar actionBar = getActionBar();
    actionBar.hide();
    setContentView(R.layout.activity_details);
     pd = ProgressDialog.show(DetailsActivity.this, "Testing",
                "Loading");
    new ImageDownload().execute("");
    //asyncDownload();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.details, menu);
    textView1 = (TextView)findViewById(R.id.textDetailPlace);
    textView2 = (TextView)findViewById(R.id.textDetailAddress );
    textView3 = (TextView)findViewById(R.id.textCapacity);
    //  textView4 = (TextView)findViewById(R.id.textDetailContactNo);
    textView5 = (TextView) findViewById(R.id.textViewDescription);

    textView1.setText(getIntent().getExtras().getString("test"));
    textView2.setText(getIntent().getExtras().getString("test2"));
    textView3.setText(getIntent().getExtras().getString("test3"));
    //textView4.setText(getIntent().getExtras().getString("test4"));
    textView5.setText(getIntent().getExtras().getString("test5"));

    return true;
}

}

2 个答案:

答案 0 :(得分:0)

setContentView(R.layout.activity_details);方法中super.onCreate(savedInstanceState);之后添加onCreate()

检查一下:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
image = (ImageView)findViewById(R.id.imageView2);
ActionBar actionBar = getActionBar();
actionBar.hide();

 pd = ProgressDialog.show(DetailsActivity.this, "Testing",
            "Loading");
new ImageDownload().execute("");
//asyncDownload();
}

答案 1 :(得分:0)

在设置内容视图后放置image = (ImageView)findViewById(R.id.imageView2);

您可以在设置activity的视图后找到一个视图。然后只有它标识id形式的xml布局。

setContentView(R.layout.activity_details);
image = (ImageView)findViewById(R.id.imageView2);