我的应用程序没有给出任何编译时错误,它确实显示了它显示的活动。这是一个带按钮的简单活动。当用户点击它时,它应该从网络下载图像。当我点击按钮时,它只是说,“不幸的是,并发已停止。”
我正在阅读android中的线程,我了解了不在UI线程中执行密集操作的规则,也没有从UI线程外部操纵UI。我正在练习这个。
我还不熟悉如何阅读logcats。我发布了我的logcat,如果有人可以阅读它并指出造成问题的原因,那将会很棒。
此外,我没有编写loadImageFromNetwork()的实现。我刚刚从互联网上复制了它。所以我对它对decodeStream()方法的调用一无所知。这不是我现在关注的问题。
Main.java: -
package com.example.concurrency;
import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
public static final String key_name="com.practice.firstApp.key";
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void downloadImage(){
new Thread(new Runnable(){
private Bitmap loadImageFromNetwork(String url){
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void run(){
final Bitmap bitmap= loadImageFromNetwork("http://www.google.com/imgres?imgurl=http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F7%2F7a%2FBasketball.png&imgrefurl=http%3A%2F%2Fcommons.wikimedia.org%2Fwiki%2FFile%3ABasketball.png&h=340&w=340&tbnid=EJmjEDyJzrhAuM%3A&zoom=1&docid=C_hn8nOgsGmuwM&hl=en&ei=Q0o2U93LNcaIygH4mICQBQ&tbm=isch&ved=0CHwQhBwwBg&iact=rc&dur=3875&page=1&start=0&ndsp=14");
imageView.post(new Runnable(){
public void run(){
imageView.setImageBitmap(bitmap);
}
});
}
}).start();
}
}
activity_main.xml中: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Button_MainActivity"
android:onClick="downloadImage"/>
</RelativeLayout>
String.xml: -
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Concurrency</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Button_MainActivity">Download</string>
</resources>
的Manifest.xml: -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.concurrency"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.concurrency.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的: -
超出了问题的长度。所以你可以找到它here。
答案 0 :(得分:0)
您的downloadImage函数需要将View作为参数。添加它,它应该修复错误
答案 1 :(得分:0)
您必须在下面添加以下内容才能获得为您的应用程序访问Internet的权限...
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
活动中的AsynchTask函数的访问方法....
BitmapWorkerTask backPropcess = new BitmapWorkerTask(position, ht,
wt);
backPropcess.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, urls);
这是一个asynctask下载类
public BitmapWorkerTask() {
}
@Override
protected Bitmap doInBackground(String... params) {
URL url;
try {
url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(4000);
connection.setReadTimeout(10000);
int v = connection.getContentLength() > 0 ? connection
.getContentLength() : 0;
if (v > 0) {
InputStream in = new BufferedInputStream(
connection.getInputStream(), 32 * 1024);
Bitmap bitmap = decodeSampledBitmapFromResource(
in, ht[i], wt[i]);
if (bitmap != null && position != null) {
addBitmapToMemoryCache(position, bitmap);
System.out.println(position);
}
} catch (MalformedURLException e) {
// e.printStackTrace();
} catch (IOException e) {
// e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
}
答案 2 :(得分:0)
您可以使用AsyncTask或以下代码
private Handler mhHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 100 && msg.obj != null) {
imageView.setImageBitmap((Bitmap) msg.obj);
}
};
};
public void downloadImage(View v) {
new Thread(new Runnable() {
private Bitmap loadImageFromNetwork(String url) {
try {
Bitmap bitmap = BitmapFactory
.decodeStream((InputStream) new URL(url)
.getContent());
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void run() {
final Bitmap bitmap = loadImageFromNetwork("http://www.google.com/imgres?imgurl=http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F7%2F7a%2FBasketball.png&imgrefurl=http%3A%2F%2Fcommons.wikimedia.org%2Fwiki%2FFile%3ABasketball.png&h=340&w=340&tbnid=EJmjEDyJzrhAuM%3A&zoom=1&docid=C_hn8nOgsGmuwM&hl=en&ei=Q0o2U93LNcaIygH4mICQBQ&tbm=isch&ved=0CHwQhBwwBg&iact=rc&dur=3875&page=1&start=0&ndsp=14");
Message msg = new Message();
msg.what = 100;
msg.obj = bitmap;
mhHandler.sendMessage(msg);
}
}).start();
}
并且不要忘记将View放在方法downloadImage(View v)的参数上,因为你从布局中调用onClick。
答案 3 :(得分:0)
添加
<uses-permission android:name="android.permission.INTERNET" />
还要添加类型&#34; View&#34;的参数。你的downloadImage函数。
答案 4 :(得分:0)
private Bitmap loadImageFromNetwork(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
e.printStackTrace();
return icon;
}
}