我是 android 应用程序的新手,我正在classNotFound exception
运行 android 应用程序。
我粘贴了 logcat、manifest 文件和 java 代码。
谁能知道问题的原因?
日志猫:-
02-25 12:10:20.071: W/dalvikvm(273): Unable to resolve superclass of Lcom/phonegap/plugins/downloader/MainActivity; (35)
02-25 12:10:20.071: W/dalvikvm(273): Link of class 'Lcom/phonegap/plugins/downloader/MainActivity;' failed
02-25 12:10:20.071: D/AndroidRuntime(273): Shutting down VM
02-25 12:10:20.071: W/dalvikvm(273): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-25 12:10:20.101: E/AndroidRuntime(273): FATAL EXCEPTION: main
02-25 12:10:20.101: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.phonegap.plugins.downloader/com.phonegap.plugins.downloader.MainActivity}: java.lang.ClassNotFoundException: com.phonegap.plugins.downloader.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.phonegap.plugins.downloader-1.apk]
02-25 12:10:20.101: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
02-25 12:10:20.101: E/AndroidRuntime(273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-25 12:10:20.101: E/AndroidRuntime(273): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-25 12:10:20.101: E/AndroidRuntime(273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-25 12:10:20.101: E/AndroidRuntime(273): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 12:10:20.101: E/AndroidRuntime(273): at android.os.Looper.loop(Looper.java:123)
02-25 12:10:20.101: E/AndroidRuntime(273): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-25 12:10:20.101: E/AndroidRuntime(273): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 12:10:20.101: E/AndroidRuntime(273): at java.lang.reflect.Method.invoke(Method.java:521)
02-25 12:10:20.101: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-25 12:10:20.101: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-25 12:10:20.101: E/AndroidRuntime(273): at dalvik.system.NativeStart.main(Native Method)
02-25 12:10:20.101: E/AndroidRuntime(273): Caused by: java.lang.ClassNotFoundException: com.phonegap.plugins.downloader.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.phonegap.plugins.downloader-1.apk]
02-25 12:10:20.101: E/AndroidRuntime(273): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
02-25 12:10:20.101: E/AndroidRuntime(273): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
02-25 12:10:20.101: E/AndroidRuntime(273): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
02-25 12:10:20.101: E/AndroidRuntime(273): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-25 12:10:20.101: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
Java代码:-
package com.phonegap.plugins.downloader;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/download/download.html");
}
}
清单文件:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phonegap.plugins.downloader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.phonegap.plugins.downloader.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>
<activity android:name="com.phonegap.plugins.downloader.Downloader" />
</application>
</manifest>
下载器.java
package com.phonegap.plugins.downloader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.os.Environment;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
public class Downloader extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
if (!action.equals("downloadFile"))
return new PluginResult(PluginResult.Status.INVALID_ACTION);
try {
String fileUrl = args.getString(0);
JSONObject params = args.getJSONObject(1);
String fileName = params.has("fileName") ?
params.getString("fileName"):
fileUrl.substring(fileUrl.lastIndexOf("/")+1);
String dirName = params.has("dirName") ?
params.getString("dirName"):
Environment.getExternalStorageDirectory().getPath() + "/download";
Boolean overwrite = params.has("overwrite") ? params.getBoolean("overwrite") : false;
return this.downloadUrl(fileUrl, dirName, fileName, overwrite, callbackId);
} catch (JSONException e) {
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
}
}
private PluginResult downloadUrl(String fileUrl, String dirName, String fileName, Boolean overwrite, String callbackId) throws InterruptedException, JSONException {
try {
Log.d("PhoneGapLog", "Downloading "+fileUrl + " into " + dirName + "/" + fileName);
File dir = new File(dirName);
if (!dir.exists()) {
Log.d("PhoneGapLog", "directory " + dirName + " created");
dir.mkdirs();
}
File file = new File(dirName, fileName);
if (!overwrite && file.exists()) {
Log.d("DownloaderPlugin", "File already exist");
JSONObject obj = new JSONObject();
obj.put("status", 1);
obj.put("total", 0);
obj.put("file", fileName);
obj.put("dir", dirName);
obj.put("progress", 100);
return new PluginResult(PluginResult.Status.OK, obj);
}
URL url = new URL(fileUrl);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setRequestMethod("GET");
ucon.connect();
Log.d("PhoneGapLog", "Download start");
InputStream is = ucon.getInputStream();
byte[] buffer = new byte[1024];
int readed = 0,
progress = 0,
totalReaded = 0,
fileSize = ucon.getContentLength();
FileOutputStream fos = new FileOutputStream(file);
while ((readed = is.read(buffer)) > 0) {
fos.write(buffer, 0, readed);
totalReaded += readed;
int newProgress = (totalReaded*100/fileSize);
if (newProgress != progress)
progress = informProgress(fileSize, newProgress, dirName, fileName, callbackId);
}
fos.close();
Log.d("PhoneGapLog", "Download finished");
JSONObject obj = new JSONObject();
obj.put("status", 1);
obj.put("total", fileSize);
obj.put("file", fileName);
obj.put("dir", dirName);
obj.put("progress", progress);
return new PluginResult(PluginResult.Status.OK, obj);
}
catch (FileNotFoundException e) {
Log.d("PhoneGapLog", "File Not Found: " + e);
return new PluginResult(PluginResult.Status.ERROR, 404);
}
catch (IOException e) {
Log.d("PhoneGapLog", "Error: " + e);
return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
}
}
private int informProgress(int fileSize, int progress, String dirName, String fileName, String callbackId) throws InterruptedException, JSONException {
JSONObject obj = new JSONObject();
obj.put("status", 0);
obj.put("total", fileSize);
obj.put("file", fileName);
obj.put("dir", dirName);
obj.put("progress", progress);
PluginResult res = new PluginResult(PluginResult.Status.OK, obj);
res.setKeepCallback(true);
success(res, callbackId);
//Give a chance for the progress to be sent to javascript
Thread.sleep(100);
return progress;
}
}