当我在模拟器中启动应用程序时,应用程序启动,但是当我插入设备时,它崩溃了。我认为它已经崩溃,因为我没有附件插件,如果这是真的,为什么它不会在模拟器中崩溃。
Main_Activity:
public class MainActivity extends Activity implements Runnable {
private static final int Message_Status_Accu = 1;
private static final int Message_Consuption = 2;
private static final int Message_temperature = 3;
private static final String TAG = "Note";
private static final String ACTION_USB_PERMISSION =
"com.android.example.USB_PERMISSION";
private UsbManager mUsbManager;
private PendingIntent mPermissionIntent;
private boolean mPermissionRequestPending;
private TextView mTextView;
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 = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); 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 = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
if (accessory != null && accessory.equals(mAccessory)) {
closeAccessory();
}
}
}
};
@Override
public Object onRetainNonConfigurationInstance() {
if (mAccessory != null) {
return mAccessory;
} else {
return super.onRetainNonConfigurationInstance();
}
}
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set the Main Activity
setContentView(R.layout.activity_main);
setupAccessory();
mTextView = (TextView) findViewById(R.id.mTextView);
mTextView.setText("There is not accessory plugged");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupAccessory(){
//Reference to USB System Service
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
//Ask for USB Permission when an Accessory connects
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);
// Register the Receiver
registerReceiver(mUsbReceiver, filter);
openAccessory(mAccessory);
}
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);
Thread thread = new Thread(null, this, "ChargeKit");
thread.start();
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;
}
}
@Override
public void run() {
int ret = 0;
byte[] buffer = new byte[16384]; //ByteArray Buffer with 16384 space.
int i;
while (true) {
try {
ret = mInputStream.read(buffer); //Read from mInputstream and stored in Buffer
} catch (IOException e) {
break;
}
i = 0;
while (i < ret) {
int len = ret - i;
if(len >=1){
Message m = Message.obtain(mHandler);
int value = (int) buffer[i];
m.obj = new ValueMsg(value);
mHandler.sendMessage(m);
}
i += 1; // number of bytes sent from arduino
}
}
}
Handler mHandler = new Handler()
{
public void handleMessage( Message msg){
final ValueMsg t = (ValueMsg) msg.obj;
mHandler.post(new Runnable() {
@Override
public void run() {
mTextView.setText(""+t.getReading()+"Its work!!!");
}
});
}
};
@Override
public void onDestroy() {
unregisterReceiver(mUsbReceiver);
super.onDestroy();
}
public class ValueMsg{
private int reading;
public ValueMsg(int reading){
this.reading = reading;
}
public int getReading(){
return reading;
}
}
}
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.anagas.usb.app" >
<uses-feature android:name="android.hardware.usb.accessory" />
<uses-sdk android:minSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/accessory_filter" />
</activity>
</application>
Logcat:
06-20 20:45:11.292 15036-15036/de.anagas.usb.app I/Process? Sending signal. PID: 15036 SIG: 9
06-20 20:50:48.752 15559-15559/de.anagas.usb.app D/AndroidRuntime? Shutting down VM
06-20 20:50:48.752 15559-15559/de.anagas.usb.app W/dalvikvm? threadid=1: thread exiting with uncaught exception (group=0x4163aba8)
06-20 20:50:48.752 15559-15559/de.anagas.usb.app E/AndroidRuntime? FATAL EXCEPTION: main
Process: de.anagas.usb.app, PID: 15559
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.anagas.usb.app/de.anagas.usb.app.MainActivity}: java.lang.IllegalArgumentException: no accessory attached
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: no accessory attached
at android.os.Parcel.readException(Parcel.java:1469)
at android.os.Parcel.readException(Parcel.java:1419)
at android.hardware.usb.IUsbManager$Stub$Proxy.openAccessory(IUsbManager.java:428)
at android.hardware.usb.UsbManager.openAccessory(UsbManager.java:308)
at de.anagas.usb.app.MainActivity.openAccessory(MainActivity.java:122)
at de.anagas.usb.app.MainActivity.setupAccessory(MainActivity.java:119)
at de.anagas.usb.app.MainActivity.onCreate(MainActivity.java:85)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
修改
我发现了问题。我在内容不起作用后设置了附件。
以下是解决方案
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupAccessory();
//Set the Main Activity
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.mTextView);
mTextView.setText("There is not accessory plugged");
}