我是所有这些 android 开发的新手,我试图找出这个异常的原因。
11-09 02:29:39.897: E/AndroidRuntime(657): FATAL EXCEPTION: main
11-09 02:29:39.897: E/AndroidRuntime(657): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.yoursite.arduinoblinkled/com.yoursite.arduinoblinkled.activity_arduino_blink_led}: java.lang.ClassNotFoundException: com.yoursite.arduinoblinkled.activity_arduino_blink_led
11-09 02:29:39.897: E/AndroidRuntime(657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
11-09 02:29:39.897: E/AndroidRuntime(657): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-09 02:29:39.897: E/AndroidRuntime(657): at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-09 02:29:39.897: E/AndroidRuntime(657): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-09 02:29:39.897: E/AndroidRuntime(657): at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 02:29:39.897: E/AndroidRuntime(657): at android.os.Looper.loop(Looper.java:137)
11-09 02:29:39.897: E/AndroidRuntime(657): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-09 02:29:39.897: E/AndroidRuntime(657): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 02:29:39.897: E/AndroidRuntime(657): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 02:29:39.897: E/AndroidRuntime(657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-09 02:29:39.897: E/AndroidRuntime(657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-09 02:29:39.897: E/AndroidRuntime(657): at dalvik.system.NativeStart.main(Native Method)
11-09 02:29:39.897: E/AndroidRuntime(657): Caused by: java.lang.ClassNotFoundException: com.yoursite.arduinoblinkled.activity_arduino_blink_led
11-09 02:29:39.897: E/AndroidRuntime(657): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-09 02:29:39.897: E/AndroidRuntime(657): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-09 02:29:39.897: E/AndroidRuntime(657): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-09 02:29:39.897: E/AndroidRuntime(657): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-09 02:29:39.897: E/AndroidRuntime(657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
11-09 02:29:39.897: E/AndroidRuntime(657): ... 11 more
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yoursite.arduinoblinkled" android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".activity_arduino_blink_led"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
<uses-library android:name="com.android.future.usb.accessory"></uses-library>
</application>
</manifest>
ArduinoBlinkLEDActivity.java
package com.example.arduinoblinkled;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.android.future.usb.UsbAccessory;
import com.android.future.usb.UsbManager;
import com.yoursite.arduinoblinkled.R;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ToggleButton;
public class ArduinoBlinkLEDActivity extends Activity {
private static final String TAG = "ArduinoAccessory";
private static final String ACTION_USB_PERMISSION = "com.google.android.DemoKit.action.USB_PERMISSION";
private UsbManager mUsbManager;
private PendingIntent mPermissionIntent;
private boolean mPermissionRequestPending;
private ToggleButton buttonLED;
UsbAccessory mAccessory;
ParcelFileDescriptor mFileDescriptor;
FileInputStream mInputStream;
FileOutputStream mOutputStream;
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbAccessory accessory = UsbManager.getAccessory(intent);
if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
openAccessory(accessory);
} else {
Log.d(TAG, "permission denied for accessory "
+ accessory);
}
mPermissionRequestPending = false;
}
} else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
UsbAccessory accessory = UsbManager.getAccessory(intent);
if (accessory != null && accessory.equals(mAccessory)) {
closeAccessory();
}
}
}
};
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arduino_blink_led);
mUsbManager = UsbManager.getInstance(this);
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
registerReceiver(mUsbReceiver, filter);
if (getLastNonConfigurationInstance() != null) {
mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
openAccessory(mAccessory);
}
setContentView(R.layout.activity_arduino_blink_led);
buttonLED = (ToggleButton) findViewById(R.id.toggleButtonLED);
}
@SuppressWarnings("deprecation")
@Override
public Object onRetainNonConfigurationInstance() {
if (mAccessory != null) {
return mAccessory;
} else {
return super.onRetainNonConfigurationInstance();
}
}
@Override
public void onResume() {
super.onResume();
if (mInputStream != null && mOutputStream != null) {
return;
}
UsbAccessory[] accessories = mUsbManager.getAccessoryList();
UsbAccessory accessory = (accessories == null ? null : accessories[0]);
if (accessory != null) {
if (mUsbManager.hasPermission(accessory)) {
openAccessory(accessory);
} else {
synchronized (mUsbReceiver) {
if (!mPermissionRequestPending) {
mUsbManager.requestPermission(accessory,mPermissionIntent);
mPermissionRequestPending = true;
}
}
}
} else {
Log.d(TAG, "mAccessory is null");
}
}
private void openAccessory(UsbAccessory accessory) {
mFileDescriptor = mUsbManager.openAccessory(accessory);
if (mFileDescriptor != null) {
mAccessory = accessory;
FileDescriptor fd = mFileDescriptor.getFileDescriptor();
mInputStream = new FileInputStream(fd);
mOutputStream = new FileOutputStream(fd);
Log.d(TAG, "accessory opened");
} else {
Log.d(TAG, "accessory open fail");
}
}
private void closeAccessory() {
try {
if (mFileDescriptor != null) {
mFileDescriptor.close();
}
} catch (IOException e) {
} finally {
mFileDescriptor = null;
mAccessory = null;
}
}
public void blinkLED(View v){
byte[] buffer = new byte[1];
if(buttonLED.isChecked())
buffer[0]=(byte)0; // button says on, light is off
else
buffer[0]=(byte)1; // button says off, light is on
if (mOutputStream != null) {
try {
mOutputStream.write(buffer);
} catch (IOException e) {
Log.e(TAG, "write failed", e);
}
}
}
}
我尝试了很多解决方案,但问题仍然没有解决。有谁知道这可能是什么原因造成的?非常感谢。