接收java.lang.RuntimeException:无法销毁活动
{com.android.systemui/com.android.systemui.usb.UsbPermissionActivity}: java.lang.NullPointerException
这是我的日志猫:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to destroy activity {com.android.systemui/com.android.systemui.usb.UsbPermissionActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3499)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3517)
at android.app.ActivityThread.access$1200(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.android.systemui.usb.UsbPermissionActivity.onDestroy(UsbPermissionActivity.java:148)
at android.app.Activity.performDestroy(Activity.java:5302)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3486)
onClickListener后的我的代码:
login.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String User_Name;
User_Name=username.getText().toString();
Device_Found=0;
if(User_Name.toLowerCase().equals("medequip") && password.getText().toString().equals("medequip"))
{
Communication_Ok=0; Permission_Error=0;
checkdeviceconnection();
if(Device_Found==1)
{
Data_Rx_Index=Data_Index=setTotal_Frame_Received(0);
if(Permission_Error==1)
checkdeviceconnection();
Receive.start();
long i,j;
for(i=0;i<1000;i++)
for(j=0;j<1000;j++);
if(Communication_Ok==1)
Toast.makeText(Medequiplogin.this, "Communication_Ok", 10000).show();
else
Toast.makeText(Medequiplogin.this, "Communication Failure", 10000).show();
Intent i3 = new Intent(Medequiplogin.this,Modes.class);
startActivity(i3);
}
else
{ Toast.makeText(Medequiplogin.this, "Device Connection Problem", 10000).show();
return;}
}
else
{
Toast.makeText(Medequiplogin.this, "Plesae Enter valid Username and Password", 10000).show();
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username.setText("");
password.setText("");
}
});
}
protected void checkdeviceconnection()
{
int i,j;
String as[];
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
i = deviceList.size();
if(i==0)
{
return;
}
as = new String[i];
deviceList.keySet().toArray(as);
for(j=0;j<i;j++)
{
Device_Detials = (UsbDevice)deviceList.get(as[j]);
if((Device_Detials.getVendorId()==targetVendorID) && (Device_Detials.getProductId()==targetProductID))
{
break;
}
}
if(Device_Detials!=null)
{
usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
if(usbManager.hasPermission(Device_Detials)==true)
{
// Toast.makeText(Medequiplogin.this, "Permission Granted", Toast.LENGTH_LONG).show();
Device_Settings(Device_Detials,j);
}
else
{
//PendingIntent intent = null;
// Toast.makeText(Medequiplogin.this, "No Permission", Toast.LENGTH_LONG).show();
usbManager.requestPermission(Device_Detials, null);
Permission_Error=1;
return;
}
Device_Settings(Device_Detials,j);
}
}
UsbDeviceConnection Device_Settings(UsbDevice device,int Port_No)
{
UsbEndpoint tOut = null;
UsbEndpoint tIn = null;
UsbInterface usbinterface = device.getInterface(Port_No);
if (usbinterface == null)
{
Toast.makeText(Medequiplogin.this, "Device Found But UnKnown Exception-1", 50000).show();
return null;
}
int tEndpointCnt = usbinterface.getEndpointCount();
if (tEndpointCnt >= 2)
{
for (int j = 0; j < tEndpointCnt; j++)
{
if (usbinterface.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
{
if (usbinterface.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT)
{
tOut = usbinterface.getEndpoint(j);
}
else if (usbinterface.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN)
{
tIn = usbinterface.getEndpoint(j);
}
}
}
if (tOut != null && tIn != null)
{
usbInterfaceFound = usbinterface;
endpointOut = tOut;
endpointIn = tIn;
deviceFound = device;
if (device != null)
{
Toast.makeText(Medequiplogin.this, device+" ", Toast.LENGTH_LONG).show();
UsbDeviceConnection connection = usbManager.openDevice(device);
if (connection != null && connection.claimInterface(usbInterfaceFound, true))
{
connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
connection.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);
usbDeviceConnection = connection;
usbDeviceConnection_For_Transfer = connection;
Communication_Byte[0]='&';
usbDeviceConnection.bulkTransfer(endpointOut,Communication_Byte, 1, 0);
Device_Found=1;
return usbDeviceConnection;
}
else
{
Toast.makeText(Medequiplogin.this, "Device Found But Unable to Open Device(Exception-3)", 50000).show();
usbDeviceConnection = null;
}
}
}
else
{
Toast.makeText(Medequiplogin.this, "Device Found But No End Points(Exception-4)", 50000).show();
}
}
else
{
Toast.makeText(Medequiplogin.this, "Device Found But No End Points(Exception-2)", 50000).show();
return null;
}
return null;
}
@SuppressWarnings("static-access")
public void Manual_Mode(int Total_Size) {
int i,j,k;
Communication_Ok=0;
Data_Rx_Index=Data_Index=setTotal_Frame_Received(0);
// Toast.makeText(Medequiplogin.this, usbDeviceConnection_For_Transfer+"", 50000).show();
Mode_Selection=0;
if(usbDeviceConnection_For_Transfer==null)
i=0;
else
i=0;
byte []dataRx_2= new byte[1];
Communication_Error=0;
for(k=0;k<5;k++)
{
dataRx_2[0]='&';
usbDeviceConnection_For_Transfer.bulkTransfer(endpointOut, dataRx_2, 1, 0);
for(i=0;i<1000;i++)
for(j=0;j<1000;j++);
if(Communication_Ok==1)
{
byte []dataRx_1= new byte[Total_Size+4];
dataRx_1[0]='*';
dataRx_1[Total_Size+4-1]='#';
if(mm.Present_Test_IS_OR_IR==1) //IR
{
dataRx_1[1]=(byte)0xA4; //for manual mode
dataRx_1[2]=(byte)0xA4; //for manual mode
}
else //IS
{
dataRx_1[1]=(byte)0xA5; //for manual mode
dataRx_1[2]=(byte)0xA5; //for manual mode
}
for(i=0;i<Total_Size;i++)
{
dataRx_1[i+3]=(byte)((mm.Checked_Buttons[(int) i])+0x30);
}
usbDeviceConnection_For_Transfer.bulkTransfer(endpointOut, dataRx_1, Total_Size+4, 0);
break;
}
}
if(k>=4)
Communication_Error=1;
}
public static int getTotal_Frame_Received()
{
return Total_Frame_Received;
}
public static int setTotal_Frame_Received(int total_Frame_Received)
{
Total_Frame_Received = total_Frame_Received;
return total_Frame_Received;
}
Thread Receive = new Thread(new Runnable()
{
@SuppressWarnings("static-access")
@Override
public void run()
{
ByteBuffer buffer = ByteBuffer.allocate(100);
UsbRequest request = new UsbRequest();
int i,j;
byte [] datarx=new byte[1];
while (true)
{
request.initialize(usbDeviceConnection, endpointIn);
request.queue(buffer, 100);
if (usbDeviceConnection.requestWait() == request)
{
if(Data_Rx_Index==0)
Data_Index=0;
for(i=0;i<100;i++)
{
Data_Recevied_From_MC [i]= buffer.get(i);
Data_Index=i;
if(Data_Recevied_From_MC [i]=='#')
break;
}
byte [] Data_Recevied_From_MC_1=new byte [i-1];
if(Data_Recevied_From_MC [0]=='&')
{
Communication_Ok=1;Data_Rx_Index=0;
}
if(Data_Recevied_From_MC [0]=='*' && Data_Recevied_From_MC [Data_Index]=='#' && Mode_Selection==1)
{
Data_Recevied_From_MC_1[0]=Data_Recevied_From_MC[1];
Data_Recevied_From_MC_1[1]=Data_Recevied_From_MC[2];
Data_Recevied_From_MC_1[2]=Data_Recevied_From_MC[3];
Data_Recevied_From_MC_1[3]=Data_Recevied_From_MC[4];
/*for(j=0;j<i;j++)
{
if(Data_Recevied_From_MC[j+2]!='#')
Data_Recevied_From_MC_1[j]= (Data_Recevied_From_MC[j+2]);
else
break;
} */
ByteBuffer buffer_1 = ByteBuffer.wrap(Data_Recevied_From_MC_1);
Resistance=buffer_1.getFloat();
setTotal_Frame_Received(1);
}
else if(Data_Recevied_From_MC [0]=='*' && Data_Recevied_From_MC [Data_Index]=='#')
{
Test_Point_No=(int)Data_Recevied_From_MC[1];
if(mm.Present_Test_IS_OR_IR==1)
{
for(j=0;j<i;j++)
{
if(Data_Recevied_From_MC[j+2]!='#')
Data_Recevied_From_MC_1[j]= (Data_Recevied_From_MC[j+2]);
else
break;
}
ByteBuffer buffer_1 = ByteBuffer.wrap(Data_Recevied_From_MC_1);
Resistance=buffer_1.getFloat();
}
else
{
Current=(int)Data_Recevied_From_MC[2];
}
setTotal_Frame_Received(1);
}
runOnUiThread(new Runnable()
{
@Override
public void run()
{
}});
}
else
{
Toast.makeText(Medequiplogin.this, "No data received", 50000).show();
break;
}
}
}
});
当我开始调试时,我在第172行遭到攻击,即在permission_error = 1 ...在第172行后显示UNFORTUNATELY SYSTEM UI在Android屏幕中停止 这是我的代码:
protected void checkdeviceconnection()
{
int i,j;
String as[];
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
i = deviceList.size();
if(i==0)
{
return;
}
as = new String[i];
deviceList.keySet().toArray(as);
for(j=0;j<i;j++)
{
Device_Detials = (UsbDevice)deviceList.get(as[j]);
if((Device_Detials.getVendorId()==targetVendorID) && (Device_Detials.getProductId()==targetProductID))
{
break;
}
}
if(Device_Detials!=null)
{
usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
if(usbManager.hasPermission(Device_Detials)==true)
{
// Toast.makeText(Medequiplogin.this, "Permission Granted", Toast.LENGTH_LONG).show();
Device_Settings(Device_Detials,j);
}
else
{
//PendingIntent intent = null;
// Toast.makeText(Medequiplogin.this, "No Permission", Toast.LENGTH_LONG).show();
usbManager.requestPermission(Device_Detials, null);
Permission_Error=1;
return;
}
Device_Settings(Device_Detials,j);
}
}
UsbDeviceConnection Device_Settings(UsbDevice device,int Port_No)
{
UsbEndpoint tOut = null;
UsbEndpoint tIn = null;
UsbInterface usbinterface = device.getInterface(Port_No);
if (usbinterface == null)
{
Toast.makeText(Medequiplogin.this, "Device Found But UnKnown Exception-1", 50000).show();
return null;
}
int tEndpointCnt = usbinterface.getEndpointCount();
if (tEndpointCnt >= 2)
{
for (int j = 0; j < tEndpointCnt; j++)
{
if (usbinterface.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
{
if (usbinterface.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT)
{
tOut = usbinterface.getEndpoint(j);
}
else if (usbinterface.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN)
{
tIn = usbinterface.getEndpoint(j);
}
}
}
if (tOut != null && tIn != null)
{
usbInterfaceFound = usbinterface;
endpointOut = tOut;
endpointIn = tIn;
deviceFound = device;
if (device != null)
{
Toast.makeText(Medequiplogin.this, device+" ", Toast.LENGTH_LONG).show();
UsbDeviceConnection connection = usbManager.openDevice(device);
if (connection != null && connection.claimInterface(usbInterfaceFound, true))
{
connection.controlTransfer(0x21, 34, 0, 0, null, 0, 0);
connection.controlTransfer(0x21, 32, 0, 0, new byte[] { (byte) 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }, 7, 0);
usbDeviceConnection = connection;
usbDeviceConnection_For_Transfer = connection;
Communication_Byte[0]='&';
usbDeviceConnection.bulkTransfer(endpointOut,Communication_Byte, 1, 0);
Device_Found=1;
return usbDeviceConnection;
}
else
{
Toast.makeText(Medequiplogin.this, "Device Found But Unable to Open Device(Exception-3)", 50000).show();
usbDeviceConnection = null;
}
}
}
else
{
Toast.makeText(Medequiplogin.this, "Device Found But No End Points(Exception-4)", 50000).show();
}
}
else
{
Toast.makeText(Medequiplogin.this, "Device Found But No End Points(Exception-2)", 50000).show();
return null;
}
return null;
}
答案 0 :(得分:1)
我遇到了同样的问题。它发生在Android 4.4.2上,但在5.0上工作正常。我可以通过以下示例来解决它:
http://developer.android.com/guide/topics/connectivity/usb/host.html
具体来说,我必须将registerReceiver移动到onCreate()而不是onStart()。
您应该尝试将代码简化为更小的测试用例,该测试用例遵循usb主页上记录的模式。