Do you know why this make my application to close unexpectedly:
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.id_weather);
//getting image form url
try {
ImageView i = (ImageView)findViewById(R.id.weather_icon);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("www.myimageurl.png").getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Button b4=(Button)findViewById(R.id.button_weather10);
b4.setOnClickListener(new View.OnClickListener() {
public void onClick(View l) {
System.out.println("Yeah");
}
});
}
});
If I comment out the second setOnClickListener, it works. Its like a setOnClick inside a SetOnClick doesn’t work.
This is my logcat:
Uncaught handler: thread main exiting due to uncaught exception
java.lang.NullPointerException
at com......MyScreen$2.onClick(MyScreen.java:133)
line 133 is my second On click
I think if you are going to call findViewById() from inside the declaration of a clickListener you’d have to use your activity.this explicitly, like so:
Otherwise I think it is going to try to call a findViewById() method of a OnClickListener class, which doesn’t exist.
It also seems like you must be trying to call setContentView() more than once. You have
inside your click listener for b2. But if you’ve not yet set the content then you’ll have no button to click. And if you have already set the content once then the second one should fail. You’re only allowed to call setContentView() once I think.