以下是我编写的第一个程序,用于理解和练习AsyncTask的工作。它是一个程序,它在工作线程中从互联网上下载图像,并将其显示在主线程中的图像视图中。 根据我阅读LogCats的有限知识,我检查了LogCat并找到了短语"由以下引起:ClassNotFoundException ..."之后我最终得出结论认为它在第56行。
1.首先,请告诉我,我是否正确阅读LogCat?如果是,则第56行为空:-S
其次请告诉我你认为这个应用程序有什么问题? 提前谢谢。
UsingAsyncTask.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.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
public class UsingAsyncTask extends Activity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.using_asynctask);
String spec="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";
URL params = null;
try {params= new URL(spec);} catch(Exception e) {}
//Bitmap result;
new MyTask().execute(params);
imageView= new ImageView(null);
}
//03015583738
private class MyTask extends AsyncTask<URL, Void, Bitmap> {
private Bitmap loadImageFromNetwork(String url){
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/*protected void onPreExecute() {
}*/
protected Bitmap doInBackground(URL... params){
//if(isCancelled()) return;
String url=params[0].toString();
final Bitmap bitmap= loadImageFromNetwork(url);
return bitmap;
}
/*protected void onProgressUpdate(Progress progress){
setProgressPercent(progress[0]);
}*/
protected void onPostExecute(Bitmap result){
imageView.setImageBitmap(result);
}
}
}
logcat的: -
04-03 05:17:21.752: D/AndroidRuntime(1447): Shutting down VM
04-03 05:17:21.752: W/dalvikvm(1447): threadid=1: thread exiting with uncaught exception
(group=0xb1ab2ba8)
04-03 05:17:21.782: E/AndroidRuntime(1447): FATAL EXCEPTION: main
04-03 05:17:21.782: E/AndroidRuntime(1447): Process: com.example.concurrency, PID: 1447
04-03 05:17:21.782: E/AndroidRuntime(1447): java.lang.RuntimeException: Unable to
instantiate activity ComponentInfo
{com.example.concurrency/com.example.concurrency.UsingAsyncTask.java}:
java.lang.ClassNotFoundException: Didn't find class
"com.example.concurrency.UsingAsyncTask.java" on path: DexPathList[[zip file
"/data/app/com.example.concurrency-1.apk"],nativeLibraryDirectories=[/data/app-
lib/com.example.concurrency-1, /system/lib]]
04-03 05:17:21.782: E/AndroidRuntime(1447): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
04-03 05:17:21.782: E/AndroidRuntime(1447): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 05:17:21.782: E/AndroidRuntime(1447): at android.app.ActivityThread.access$800
(ActivityThread.java:135)
04-03 05:17:21.782: E/AndroidRuntime(1447): at android.app.ActivityThread
$H.handleMessage(ActivityThread.java:1196)
04-03 05:17:21.782: E/AndroidRuntime(1447): at android.os.Handler.dispatchMessage
(Handler.java:102)
04-03 05:17:21.782: E/AndroidRuntime(1447): at android.os.Looper.loop(Looper.java:136)
04-03 05:17:21.782: E/AndroidRuntime(1447): at android.app.ActivityThread.main
(ActivityThread.java:5017)
04-03 05:17:21.782: E/AndroidRuntime(1447): at java.lang.reflect.Method.invokeNative
(Native Method)
04-03 05:17:21.782: E/AndroidRuntime(1447): at java.lang.reflect.Method.invoke
(Method.java:515)
04-03 05:17:21.782: E/AndroidRuntime(1447): at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 05:17:21.782: E/AndroidRuntime(1447): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:595)
04-03 05:17:21.782: E/AndroidRuntime(1447): at dalvik.system.NativeStart.main(Native
Method)
04-03 05:17:21.782: E/AndroidRuntime(1447): Caused by: java.lang.ClassNotFoundException:
Didn't find class "com.example.concurrency.UsingAsyncTask.java" on path: DexPathList[[zip
file "/data/app/com.example.concurrency-1.apk"],nativeLibraryDirectories=[/data/app-
lib/com.example.concurrency-1, /system/lib]]
04-03 05:17:21.782: E/AndroidRuntime(1447): at
dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
04-03 05:17:21.782: E/AndroidRuntime(1447): at java.lang.ClassLoader.loadClass
(ClassLoader.java:497)
04-03 05:17:21.782: E/AndroidRuntime(1447): at java.lang.ClassLoader.loadClass
(ClassLoader.java:457)
04-03 05:17:21.782: E/AndroidRuntime(1447): at android.app.Instrumentation.newActivity
(Instrumentation.java:1061)
04-03 05:17:21.782: E/AndroidRuntime(1447): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
04-03 05:17:21.782: E/AndroidRuntime(1447): ... 11 more
04-03 05:17:26.782: I/Process(1447): Sending signal. PID: 1447 SIG: 9
清单文件: -
<?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-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.concurrency.UsingAsyncTask"
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>
答案 0 :(得分:1)
将AndroidManifest.xml
中的启动器活动名称更改为.UsingAsyncTask
。它目前是MainActivity
,似乎缺失了。
此外,在LogCat中提及该行时,您不必手动查找该行。只需双击Eclipse的LogCat视图中的右侧行,就会自动跳转到代码中的相应行。