我正在尝试从android示例编辑手势构建器代码。有一天我发现了这个noClassDefFound异常。
错误日志
04-04 02:13:51.278: E/AndroidRuntime(1002): FATAL EXCEPTION: main
04-04 02:13:51.278: E/AndroidRuntime(1002): java.lang.NoClassDefFoundError: android.os.AsyncTask
04-04 02:13:51.278: E/AndroidRuntime(1002): at com.likith.gesturebuilder.GestureBuilderActivity.loadGestures(GestureBuilderActivity.java:250)
04-04 02:13:51.278: E/AndroidRuntime(1002): at com.likith.gesturebuilder.GestureBuilderActivity.onCreate(GestureBuilderActivity.java:168)
04-04 02:13:51.278: E/AndroidRuntime(1002): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 02:13:51.278: E/AndroidRuntime(1002): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-04 02:13:51.278: E/AndroidRuntime(1002): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-04 02:13:51.278: E/AndroidRuntime(1002): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-04 02:13:51.278: E/AndroidRuntime(1002): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-04 02:13:51.278: E/AndroidRuntime(1002): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 02:13:51.278: E/AndroidRuntime(1002): at android.os.Looper.loop(Looper.java:123)
04-04 02:13:51.278: E/AndroidRuntime(1002): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-04 02:13:51.278: E/AndroidRuntime(1002): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 02:13:51.278: E/AndroidRuntime(1002): at java.lang.reflect.Method.invoke(Method.java:507)
04-04 02:13:51.278: E/AndroidRuntime(1002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-04 02:13:51.278: E/AndroidRuntime(1002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-04 02:13:51.278: E/AndroidRuntime(1002): at dalvik.system.NativeStart.main(Native Method)
代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
switch (requestCode)
{
case REQUEST_NEW_GESTURE:
loadGestures();
break;
}
}
}
private void loadGestures()
{
if (mTask != null && mTask.getStatus() != GesturesLoadTask.Status.FINISHED)
{
mTask.cancel(true);
}
mTask = (GesturesLoadTask) new GesturesLoadTask().execute();
}
public class GesturesLoadTask extends AsyncTask<Void, NamedGesture, Integer>
{
private int mThumbnailSize;
private int mThumbnailInset;
private int mPathColor;
@Override
protected void onPreExecute()
{
super.onPreExecute();
final SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
final Resources resources = getResources();
//mPathColor = resources.getColor(R.color.gesture_color);
mPathColor = pref.getInt("gesture_color", Color.parseColor("#ffe3a0"));
mThumbnailInset = (int) resources.getDimension(R.dimen.gesture_thumbnail_inset);
mThumbnailSize = (int) resources.getDimension(R.dimen.gesture_thumbnail_size);
findViewById(R.id.addButton).setEnabled(false);
mAdapter.setNotifyOnChange(false);
mAdapter.clear();
}
@Override
protected Integer doInBackground(Void... params)
{
if (isCancelled()) return STATUS_CANCELLED;
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
return STATUS_NO_STORAGE;
}
final GestureLibrary store = sStore;
if (store.load())
{
for (String name : store.getGestureEntries())
{
if (isCancelled()) break;
for (Gesture gesture : store.getGestures(name))
{
final Bitmap bitmap = gesture.toBitmap(mThumbnailSize, mThumbnailSize,mThumbnailInset, mPathColor);
final NamedGesture namedGesture = new NamedGesture();
namedGesture.gesture = gesture;
namedGesture.name = name;
mAdapter.addBitmap(namedGesture.gesture.getID(), bitmap);
publishProgress(namedGesture);
}
}
return STATUS_SUCCESS;
}
return STATUS_NOT_LOADED;
}
@Override
protected void onProgressUpdate(NamedGesture... values)
{
super.onProgressUpdate(values);
final GesturesAdapter adapter = mAdapter;
adapter.setNotifyOnChange(false);
for (NamedGesture gesture : values)
{
adapter.add(gesture);
tap_info.setVisibility(View.VISIBLE);
}
adapter.sort(mSorter);
adapter.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Integer result)
{
super.onPostExecute(result);
if (result == STATUS_NO_STORAGE)
{
getListView().setVisibility(View.GONE);
mEmpty.setVisibility(View.VISIBLE);
mEmpty.setText(getString(R.string.gestures_error_loading,mStoreFile.getAbsolutePath()));
}
else
{
findViewById(R.id.addButton).setEnabled(true);
checkForEmpty();
}
}
}
noClassDefFound异常是什么意思以及如何解决它。 请帮忙。
答案 0 :(得分:1)
NoClassException意味着在类路径中的任何位置都找不到类,在本例中为AsyncTask。
查看此主题的一些解决方案: Android: NoClassDefFoundError android.os.AsyncTask
其中一个使用Class.forName(String)调用手动加载类。