Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android设备的USB检测代码正在崩溃_Java_Android_Usb - Fatal编程技术网

Java android设备的USB检测代码正在崩溃

Java android设备的USB检测代码正在崩溃,java,android,usb,Java,Android,Usb,我已经为android设备编写了一个usb检测的小代码,但是当我将设备电缆连接到电脑的CPU时,它崩溃了 这是我的密码 public class IntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if(int

我已经为android设备编写了一个usb检测的小代码,但是当我将设备电缆连接到电脑的CPU时,它崩溃了

这是我的密码

public class IntentReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // TODO Auto-generated method stub

        if(intent.getAction().equals(Intent.ACTION_UMS_CONNECTED))

        {

            Toast.makeText(context, "mounted", Toast.LENGTH_LONG).show();
            Log.i("ANDROID DEVICE IS","CONNECTED");
            System.out.println(" ANDROID DEVICE IS CONNECTED");
            // Intent myStarterIntent = new Intent(context, CarHome.class);
            //   myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //   context.startActivity(myStarterIntent);
        }

        else if(intent.getAction().equals(Intent.ACTION_UMS_DISCONNECTED)){

            Toast.makeText(context, "Unmounted", Toast.LENGTH_LONG).show();
            Log.i("ANDROID DEVICE IS","DISCONNECTED");  
            System.out.println(" ANDROID DEVICE IS NOT CONNECTED");
        }
    }

}
清单文件在这里

<receiver android:name=".IntentReceiver">
    <intent-filter>
      <action android:name="android.intent.action.ACTION_UMS_CONNECTED" />
      <action android:name="android.intent.action.ACTION_UMS_DISCONNECTED" />
      <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
    </intent-filter>
</receiver>
最新日志信息

05-25 17:51:32.878: E/AndroidRuntime(5602): FATAL EXCEPTION: main
05-25 17:51:32.878: E/AndroidRuntime(5602): java.lang.RuntimeException: Unable to instantiate receiver com.kk88655.hello.IntentReceiver: java.lang.ClassNotFoundException: com.kk88655.hello.IntentReceiver in loader dalvik.system.PathClassLoader[/data/app/com.kk88655.hello-1.apk]
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1785)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.access$2400(ActivityThread.java:121)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.os.Looper.loop(Looper.java:130)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.main(ActivityThread.java:3701)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.reflect.Method.invoke(Method.java:507)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at dalvik.system.NativeStart.main(Native Method)
05-25 17:51:32.878: E/AndroidRuntime(5602): Caused by: java.lang.ClassNotFoundException: com.kk88655.hello.IntentReceiver in loader dalvik.system.PathClassLoader[/data/app/com.kk88655.hello-1.apk]
05-25 17:51:32.878: E/AndroidRuntime(5602):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1776)
05-25 17:51:32.878: E/AndroidRuntime(5602):     ... 10 more
05-25 17:51:32.878: W/ActivityManager(238):   Force finishing activity com.kk88655.hello/.CarHome

您应该阅读以下文档:

行动(已连接)

此常量已弃用。取代
android.os.storage.StorageEventListener

广播操作:设备已进入USB大容量存储模式。这 主要用于USB设置面板。应用程序应该监听 已安装和未安装的动作媒体广播 安装或卸载SD卡文件系统时发出通知


针对您的
ClassNotFoundException

在清单中,在BoradCastereceiver的类名前面加上句号,这是一种快捷方式。它将采用您在
-tags
package
-属性中指定的包名,并将给定的类名添加到其中

因此,您应该确保您的完全限定类路径是
com.kk88655.hello.IntentReceiver
,因为您似乎将
hello
-包重命名为
hello2
(如日志中所示:
/data/app/com.kk88655.hello-2.apk


这里已经有人提出了一个类似的问题:到目前为止还没有被接受或赞成的答案

另一个想法是在
-tags
android:name
-属性中指定完全限定的包名,如下所示:


另一种解释可能是此处讨论的旧应用程序版本的更新问题:阅读此问题,它也可能是App2SD问题。

非常感谢您提供的信息,我尝试了您的建议,但对meError无效?行为?请更具体一点。你能发布完整的stacktrace吗?它应该以类似
java.lang.XXX-Exception:[问题]
的内容开始。我在问题中发布了最新的logcat消息。我做了更多的研究并更新了我的答案。请检查这是否对您有帮助。否则,我想这是一个Android错误。我发现如果你仍然有问题,这个[post][1]很有用。[1] :@Hannes,谢谢你的链接,问题已经解决了。
05-25 17:51:32.878: E/AndroidRuntime(5602): FATAL EXCEPTION: main
05-25 17:51:32.878: E/AndroidRuntime(5602): java.lang.RuntimeException: Unable to instantiate receiver com.kk88655.hello.IntentReceiver: java.lang.ClassNotFoundException: com.kk88655.hello.IntentReceiver in loader dalvik.system.PathClassLoader[/data/app/com.kk88655.hello-1.apk]
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1785)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.access$2400(ActivityThread.java:121)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.os.Looper.loop(Looper.java:130)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.main(ActivityThread.java:3701)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.reflect.Method.invokeNative(Native Method)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.reflect.Method.invoke(Method.java:507)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at dalvik.system.NativeStart.main(Native Method)
05-25 17:51:32.878: E/AndroidRuntime(5602): Caused by: java.lang.ClassNotFoundException: com.kk88655.hello.IntentReceiver in loader dalvik.system.PathClassLoader[/data/app/com.kk88655.hello-1.apk]
05-25 17:51:32.878: E/AndroidRuntime(5602):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-25 17:51:32.878: E/AndroidRuntime(5602):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1776)
05-25 17:51:32.878: E/AndroidRuntime(5602):     ... 10 more
05-25 17:51:32.878: W/ActivityManager(238):   Force finishing activity com.kk88655.hello/.CarHome