UsbManager返回空指针异常 我在这里附上了我的代码。请任何人帮助我。
05-14 16:07:38.312: E/AndroidRuntime(27314): FATAL EXCEPTION: main
05-14 16:07:38.312: E/AndroidRuntime(27314): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ims.act/com.ims.act.ConfirmView}: java.lang.NullPointerException
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.app.ActivityThread.access$600(ActivityThread.java:128)
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.os.Looper.loop(Looper.java:137)
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.app.ActivityThread.main(ActivityThread.java:4517)
05-14 16:07:38.312: E/AndroidRuntime(27314): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 16:07:38.312: E/AndroidRuntime(27314): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 16:07:38.312: E/AndroidRuntime(27314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
05-14 16:07:38.312: E/AndroidRuntime(27314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
05-14 16:07:38.312: E/AndroidRuntime(27314): at dalvik.system.NativeStart.main(Native Method)
05-14 16:07:38.312: E/AndroidRuntime(27314): Caused by: java.lang.NullPointerException
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.os.Parcel.readException(Parcel.java:1333)
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.os.Parcel.readException(Parcel.java:1281)
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.hardware.usb.IUsbManager$Stub$Proxy.requestDevicePermission(IUsbManager.java:535)
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.hardware.usb.UsbManager.requestPermission(UsbManager.java:361)
05-14 16:07:38.312: E/AndroidRuntime(27314): at com.ims.act.ConfirmView.UsbPrinter(ConfirmView.java:92)
05-14 16:07:38.312: E/AndroidRuntime(27314): at com.ims.act.ConfirmView.onCreate(ConfirmView.java:77)
05-14 16:07:38.312: E/AndroidRuntime(27314): at android.app.Activity.performCreate(Activity.java:4470)
现在,我发布了一个确认视图。请帮帮我.usbManager.requestPermission(device,mPermissionIntent)返回NullPointerException
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.confirmpage);
System.out.println("test");
inData = getIntent().getStringExtra("ShowText");
Request = getIntent().getStringExtra("Request");
UID = getIntent().getStringExtra("UID");
ReqType = getIntent().getIntExtra("ReqType", 0);
SubType = getIntent().getStringExtra("SubType");
Receipt = getIntent().getStringExtra("Receipt");
System.out.println("receipt:" + Receipt);
Log.d("CONFIRMPAGE", Request);
StaticTranser.textView = (TextView) findViewById(R.id.status);
StaticTranser.textView.setText(inData);
StaticTranser.textView.setTypeface(Typeface.MONOSPACE);
UsbPrinter();
}
private void UsbPrinter() {
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
Iterator<UsbDevice> it = deviceList.values().iterator();
while (it.hasNext()) {
device = it.next();
System.out.println("Devices :" + device.toString());
}
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this,0,
new Intent(ACTION_USB_PERMISSION), 0);
usbManager.requestPermission(device, mPermissionIntent);
registerReceiver(mUsbReceiver, filter);
}
@Override
protected void onResume() {
registerReceiver(mUsbReceiver, filter);
super.onResume();
}
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) {
device = (UsbDevice) intent
.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
getConnection();
}
} else {
Log.d("BROADCOST RECEIVER",
"permission denied for device " + device);
}
if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
if (device != null) {
Log.d("CLOSE USB Connection", "Connection closed");
connection.releaseInterface(intf);
connection.close();
}
}
}
}
}
};
我正在测试三星移动版4.0。我正在使用我的手机连接usb打印机,但它将返回NullPointerException。