gpt4 book ai didi

java - 出现异常如何解决

转载 作者:行者123 更新时间:2023-12-02 20:01:55 25 4
gpt4 key购买 nike

我正在开发 Android 应用程序。我是 Android 新手,正在开发蓝牙应用程序。当我单击按钮时,我的项目出现错误,我的应用程序崩溃并出现目录中给出的异常。这是 XML 布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Bluetooth"
android:id="@+id/btnBluetooth"
android:onClick="getBluetoothActivity"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

</RelativeLayout>

这是代码。

public void getBluetoothActivity(View view) {
Intent intent = new Intent(getApplicationContext(), BluetoothActivity.class);
startActivity(intent); //exception
}

还有日志,

04-15 21:48:24.760 126-332/? I/ActivityManager﹕ Starting activity: Intent { cmp=com.example.app/.BluetoothActivity } from pid 25365 04-15 21:48:24.770 25365-25365/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:2191) at android.view.View.performClick(View.java:2532) at android.view.View$PerformClick.run(View.java:9293) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:143) at android.app.ActivityThread.main(ActivityThread.java:4277) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at android.view.View$1.onClick(View.java:2186) at android.view.View.performClick(View.java:2532) at android.view.View$PerformClick.run(View.java:9293) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:143) at android.app.ActivityThread.main(ActivityThread.java:4277) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method) Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.app/com.example.app.BluetoothActivity}; have you declared this activity in your AndroidManifest.xml? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1541) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1515) at android.app.Activity.startActivityForResult(Activity.java:2988) at android.app.Activity.startActivity(Activity.java:3094) at com.example.app.MainActivity.getBluetoothActivity(MainActivity.java:21) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at android.view.View$1.onClick(View.java:2186) at android.view.View.performClick(View.java:2532) at android.view.View$PerformClick.run(View.java:9293) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:143) at android.app.ActivityThread.main(ActivityThread.java:4277) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method)

我正在 Android Studio 中开发蓝牙应用程序当点击按钮时出现异常并且应用程序崩溃。

最佳答案

无法找到显式 Activity 类 {com.example.app/com.example.app.BluetoothActivity};您是否在 AndroidManifest.xml 中声明了此 Activity

这意味着 BluetoothActivity 未在您的 list 中声明。如果您认为已经如此,请验证您的包名称是否仍与声明的名称匹配( list 中的 android:namepackage)。

我在您的问题中搜索了“由”一词。这通常是查找抛出异常的位置和原因的最佳方法。

关于java - 出现异常如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29656957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com