如何在“ BluetoothLeGatt”示例之前添加活动?

时间:2019-02-18 06:53:07

标签: android android-activity bluetooth-lowenergy android-bluetooth bluetooth-gatt

作为编程的新手,我采用了Android Studio示例代码“ BluetoothLeGatt”,整个项目本身都可以正常工作。但是,当我尝试添加活动(我添加了一个Java文件,在该按钮上我也使用新的xml文件创建的按钮)后,我在其中正确地使用了意图来打开BluetoothLeGatt的第一个活动(即DeviceScanActivity.java)。我将此新活动作为启动器放在清单文件中,并将DeviceScanActivity放在清单中下面。在编译过程中,没有错误,但是当我在手机上打开apk时,应用程序立即崩溃了,我该怎么做?

TestLaunchActivity.java(应该是第一个新的)

package com.example.android.bluetoothlegatt;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;



            public class TestLaunchActivity extends AppCompatActivity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.testlaunchactivity);

                    Button button_startBleGatt = (Button) findViewById(R.id.button_startBleGatt);

                    button_startBleGatt.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View view) {
                            Intent gotoselection = new Intent(TestLaunchActivity.this, DeviceScanActivity.class);
                            startActivity(gotoselection);
                        }
                    });
                }
            }

testlaunchactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<Button
    android:id="@+id/button_startBleGatt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="80dp"
    android:text="start"
    android:textAllCaps="true" />


</LinearLayout>

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.bluetoothlegatt"
android:versionCode="1"
android:versionName="1.0">

<!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->

<!-- Declare this required feature if you want to make the app available to BLE-capable
devices only.  If you want to make your app available to devices that don't support BLE,
you should omit this in the manifest.  Instead, determine BLE capability by using
PackageManager.hasSystemFeature(FEATURE_BLUETOOTH_LE) -->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<application android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@android:style/Theme.Holo.Light">

    <activity android:name=".TestLaunchActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    <activity android:name=".DeviceControlActivity"/>
    <activity android:name=".DeviceScanActivity"/>
    <service android:name=".BluetoothLeService" android:enabled="true"/>

</application>

</manifest>

Logcat:

02-18 11:56:18.765 3331-3331/? D/dalvikvm: Late-enabling CheckJNI 02-18 11:56:18.815 3331-3331/com.example.android.bluetoothlegatt D/ActivityThread: handleBindApplication:com.example.android.bluetoothlegatt 02-18 11:56:18.815 3331-3331/com.example.android.bluetoothlegatt D/ActivityThread: setTargetHeapUtilization:0.75 02-18 11:56:18.815 3331-3331/com.example.android.bluetoothlegatt D/ActivityThread: setTargetHeapMinFree:2097152 02-18 11:56:18.815 3331-3331/com.example.android.bluetoothlegatt W/ActivityThread: Application com.example.android.bluetoothlegatt is waiting for the debugger on port 8100... 02-18 11:56:18.825 3331-3331/com.example.android.bluetoothlegatt I/System.out: Sending WAIT chunk 02-18 11:56:18.995 3331-3337/com.example.android.bluetoothlegatt I/dalvikvm: Debugger is active 02-18 11:56:19.025 3331-3331/com.example.android.bluetoothlegatt I/System.out: Debugger has connected 02-18 11:56:19.025 3331-3331/com.example.android.bluetoothlegatt I/System.out: waiting for debugger to settle... 02-18 11:56:19.225 3331-3331/com.example.android.bluetoothlegatt I/System.out: waiting for debugger to settle... 02-18 11:56:19.425 3331-3331/com.example.android.bluetoothlegatt I/System.out: waiting for debugger to settle... 02-18 11:56:19.625 3331-3331/com.example.android.bluetoothlegatt I/System.out: waiting for debugger to settle... 02-18 11:56:19.825 3331-3331/com.example.android.bluetoothlegatt I/System.out: waiting for debugger to settle... 02-18 11:56:20.025 3331-3331/com.example.android.bluetoothlegatt I/System.out: waiting for debugger to settle... 02-18 11:56:20.225 3331-3331/com.example.android.bluetoothlegatt I/System.out: waiting for debugger to settle... 02-18 11:56:20.425 3331-3331/com.example.android.bluetoothlegatt I/System.out: debugger has settled (1315) 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.view.Window$Callback.onPointerCaptureChanged, referenced from method android.support.v7.view.WindowCallbackWrapper.onPointerCaptureChanged 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve interface method 15186: Landroid/view/Window$Callback;.onPointerCaptureChanged (Z)V 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve interface method 15188: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve interface method 15190: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve interface method 15194: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 783: Landroid/content/res/TypedArray;.getChangingConfigurations ()I 02-18 11:56:20.505 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 02-18 11:56:20.515 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 02-18 11:56:20.515 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 805: Landroid/content/res/TypedArray;.getType (I)I 02-18 11:56:20.515 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.Context.createDeviceProtectedStorageContext, referenced from method android.support.v4.content.ContextCompat.createDeviceProtectedStorageContext 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 564: Landroid/content/Context;.createDeviceProtectedStorageContext ()Landroid/content/Context; 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.Context.getCodeCacheDir, referenced from method android.support.v4.content.ContextCompat.getCodeCacheDir 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 570: Landroid/content/Context;.getCodeCacheDir ()Ljava/io/File; 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.Context.getColor, referenced from method android.support.v4.content.ContextCompat.getColor 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 571: Landroid/content/Context;.getColor (I)I 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v4.content.ContextCompat.getColorStateList 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 572: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList; 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.Context.getDataDir, referenced from method android.support.v4.content.ContextCompat.getDataDir 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 574: Landroid/content/Context;.getDataDir ()Ljava/io/File; 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.Context.getDrawable, referenced from method android.support.v4.content.ContextCompat.getDrawable 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 575: Landroid/content/Context;.getDrawable (I)Landroid/graphics/drawable/Drawable; 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.Context.getNoBackupFilesDir, referenced from method android.support.v4.content.ContextCompat.getNoBackupFilesDir 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 582: Landroid/content/Context;.getNoBackupFilesDir ()Ljava/io/File; 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.Context.isDeviceProtectedStorage, referenced from method android.support.v4.content.ContextCompat.isDeviceProtectedStorage 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 595: Landroid/content/Context;.isDeviceProtectedStorage ()Z 02-18 11:56:20.545 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 02-18 11:56:20.555 3331-3331/com.example.android.bluetoothlegatt I/dalvikvm: Could not find method android.content.Context.startForegroundService, referenced from method android.support.v4.content.ContextCompat.startForegroundService 02-18 11:56:20.555 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: VFY: unable to resolve virtual method 610: Landroid/content/Context;.startForegroundService (Landroid/content/Intent;)Landroid/content/ComponentName; 02-18 11:56:20.555 3331-3331/com.example.android.bluetoothlegatt D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 02-18 11:56:20.565 3331-3331/com.example.android.bluetoothlegatt D/AndroidRuntime: Shutting down VM 02-18 11:56:20.565 3331-3331/com.example.android.bluetoothlegatt W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x415a0d58) 02-18 11:56:20.575 3331-3331/com.example.android.bluetoothlegatt E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.bluetoothlegatt, PID: 3331 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bluetoothlegatt/com.example.android.bluetoothlegatt.TestLaunchActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236) at android.app.ActivityThread.access$800(ActivityThread.java:138) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5095) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354) at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at com.example.android.bluetoothlegatt.TestLaunchActivity.onCreate(TestLaunchActivity.java:15) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236) at android.app.ActivityThread.access$800(ActivityThread.java:138) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5095) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) at dalvik.system.NativeStart.main(Native Method) 02-18 11:57:07.985 3331-3331/com.example.android.bluetoothlegatt I/Process: Sending signal. PID: 3331 SIG: 9

1 个答案:

答案 0 :(得分:0)

这是logcat-

中的重要部分
FATAL EXCEPTION: main
Process: com.example.android.bluetoothlegatt, PID: 3331
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bluetoothlegatt/com.example.android.bluetoothlegatt.TestLaunchActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

您正在为应用程序使用Theme.Holo.Light,但是您的TestLaunchActivity扩展了AppCompatActivity,它继承了Theme.AppCompat。将您的应用程序主题更改为Theme.AppCompat及其子主题(Light/Dark),这将更改操作栏的外观,并针对所有活动扩展AppCompatActivityor扩展{ Activity中的{1}}。 TestLaunchActivity用于支持AppCompatActivity的旧版本。

这里有更多资源-You need to use a Theme.AppCompat theme (or descendant) with this activity

除此之外,Android APIs对日志具有多个重要性级别。您可以在logcat中使用过滤器(debug/info/error等)。每当应用崩溃时,您都会看到相关的logcat错误。此外,错误以红色打印,因此易于识别。