使用未捕获的异常获取线程退出(group = 0x41a03700) 我想查看连接的usb设备列表,但我的应用程序崩溃请告诉我我错误的地方
这是我的代码:
package com.example.demo2c1testpanel;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Iterator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Loginpage extends Activity
{
public static final int targetVendorID = 6790;
public static final int targetProductID = 29987;
HashMap<String, UsbDevice> devicelist= null;
public UsbManager manager;
public UsbDeviceConnection usbDeviceConnection;
public UsbInterface usbInterfaceFound = null;
public UsbEndpoint endpointOut = null;
public UsbEndpoint endpointIn = null;
public UsbDevice usbdevice;
Button clear,signin;
EditText username,password;
String i = "";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
signin = (Button)findViewById(R.id.btn_signin);
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
check_connection();
}
});
}
public void check_connection()
{
manager = (UsbManager)getSystemService(Context.USB_SERVICE);
devicelist = manager.getDeviceList();
if(devicelist!=null)
{
if(usbdevice.getProductId()==targetProductID && usbdevice.getVendorId()==targetVendorID)
{
Toast.makeText(Loginpage.this, "Device connected", Toast.LENGTH_LONG).show();
}
}
else
Toast.makeText(Loginpage.this, "No Device connected", Toast.LENGTH_LONG).show();
}
}
这是我的logcat:
01-02 02:23:55.990: W/dalvikvm(8163): threadid=1: thread exiting with uncaught exception (group=0x41a03700)
01-02 02:23:56.000: E/AndroidRuntime(8163): FATAL EXCEPTION: main
01-02 02:23:56.000: E/AndroidRuntime(8163): java.lang.NullPointerException
01-02 02:23:56.000: E/AndroidRuntime(8163): at com.example.demo2c1testpanel.Loginpage.check_connection(Loginpage.java:72)
01-02 02:23:56.000: E/AndroidRuntime(8163): at com.example.demo2c1testpanel.Loginpage$1.onClick(Loginpage.java:57)
01-02 02:23:56.000: E/AndroidRuntime(8163): at android.view.View.performClick(View.java:4240)
01-02 02:23:56.000: E/AndroidRuntime(8163): at android.view.View$PerformClick.run(View.java:17721)
01-02 02:23:56.000: E/AndroidRuntime(8163): at android.os.Handler.handleCallback(Handler.java:730)
01-02 02:23:56.000: E/AndroidRuntime(8163): at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 02:23:56.000: E/AndroidRuntime(8163): at android.os.Looper.loop(Looper.java:137)
01-02 02:23:56.000: E/AndroidRuntime(8163): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-02 02:23:56.000: E/AndroidRuntime(8163): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 02:23:56.000: E/AndroidRuntime(8163): at java.lang.reflect.Method.invoke(Method.java:525)
01-02 02:23:56.000: E/AndroidRuntime(8163): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-02 02:23:56.000: E/AndroidRuntime(8163): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 02:23:56.000: E/AndroidRuntime(8163): at dalvik.system.NativeStart.main(Native Method)
我的宣言:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo2c1testpanel"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Homescreen"
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=".Loginpage"
android:label="@string/title_activity_loginpage" >
</activity>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
<activity
android:name=".Testspage"
android:label="@string/title_activity_testpage" >
</activity>
</application>
</manifest>
这是我在onClickListener之后的工作代码:
public void check_connection()
{
int i,j;
String as[];
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
i = deviceList.size();
if(i==0)
{
Toast.makeText(Loginpage.this,"Device Not Connected",Toast.LENGTH_LONG).show();
return;
}
as = new String[i];
deviceList.keySet().toArray(as);
for(j=0;j<i;j++)
{
device_details = (UsbDevice)deviceList.get(as[j]);
if((device_details.getVendorId()==targetVendorID) && (device_details.getProductId()==targetProductID))
{
Toast.makeText(Loginpage.this,"Device Connected",Toast.LENGTH_LONG).show();
}
}
}