force close when assign onclick to button
Posted
by Lynnooi
on Stack Overflow
See other posts from Stack Overflow
or by Lynnooi
Published on 2009-12-21T09:27:24Z
Indexed on
2010/06/07
1:02 UTC
Read the original article
Hit count: 446
android
|buttonclick
hi,
i am very new in android development as well as in java. i had developed an application that gets an image url from a site and wanted to download it into the device and later on i would like to enable users to set it as wallpapers. however, i am met a problem when assigning onclick event to a button. Once i uncomment the line in red, it will pop up a box stating that the application was stopped unexpectedly. Can someone please help me with this?
private ImageView imView = null;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
try {
/* Create a URL we want to load some xml-data from. */
URL url = new URL(xmlURL);
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader */
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */
/* Our ExampleHandler now provides the parsed data to us. */
ParsedExampleDataSet parsedExampleDataSet = myExampleHandler
.getParsedData();
/* Set the result to be displayed in our GUI. */
if (myExampleHandler.filenames != null) {
a = a + "\n" + myExampleHandler.filenames + ", by "
+ myExampleHandler.authors + "\nhits: "
+ myExampleHandler.hits + " downloads";
this.ed = myExampleHandler.thumbs;
this.imageURL = myExampleHandler.mediafiles;
}
} catch (Exception e) {
a = e.getMessage();
}
// get thumbnail
Context context = this.getBaseContext();
if (ed.length() != 0) {
Drawable image = ImageOperations(context, this.ed, "image.jpg");
ImageView imgView = new ImageView(context);
imgView = (ImageView) findViewById(R.id.image1);
imgView.setImageDrawable(image);
}
TextView tv = (TextView) findViewById(R.id.txt_name);
tv.setText(a);
Button bt3 = (Button) findViewById(R.id.get_imagebt);
//bt3.setOnClickListener(getImageBtnOnClick);
}
OnClickListener getImageBtnOnClick = new OnClickListener() {
public void onClick(View view) {
downloadFile(imageURL);
}
};
void downloadFile(String fileUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
// this.imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Drawable ImageOperations(Context ctx, String url,
String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/viewgroup">
<ImageView android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ImageView android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<Button id="@+id/get_imagebt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xxx Get an image"
android:layout_gravity="center" />
<ImageView id="@+id/imview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
© Stack Overflow or respective owner