我在Android开发和Java方面都很新。我开发了一个应用程序,它从一个站点获取一个图像URL,并希望将其下载到设备中,稍后我想让用户将其设置为壁纸。但是,当我将onclick事件分配给按钮时遇到问题。
一旦我将该线路取消注释为红色,它将弹出一个框,表明应用程序意外停止。有人可以帮帮我吗?
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>
答案 0 :(得分:0)
请检查catlog视图以获取错误消息。如果它是nullPointerException,那么id“get_imagebt”的按钮可能不是您设置为内容视图的文件的一部分?你也可以发布你的main.xml布局文件吗? 此致!
答案 1 :(得分:0)
您发布的代码未显示downloadFile()函数;如果你粘贴它也会有所帮助。另外,您是否可以指定要获取的异常(检查logcat)?
我能想到的一个问题可能是您的清单文件中缺少INTERNET权限。