Domanda

i want to download image from internet ...but i am getting null pointer exception i also want to know major difference bw URL connection and HTTP connection if there are any

Pls tell tell me the solution
here is my code Thank u

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;
}

}

È stato utile?

Soluzione

Put image = (ImageView)findViewById(R.id.imageView2); after set content View.

You can find a view after setting view for activity.then only its identifies the id form xml layout.

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

Altri suggerimenti

Add setContentView(R.layout.activity_details); after super.onCreate(savedInstanceState); in your onCreate() method.

Check this :

@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();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top