如何在 "BluetoothLeGatt" 示例之前添加 activity?

How to add an activity prior to the "BluetoothLeGatt" example?

作为编程新手,我使用了 "BluetoothLeGatt" 的 Android Studio 示例代码,整个项目本身可以正常运行。但是,当我尝试添加一个 activity(我添加了一个 java 文件时,我在其中正确地使用了一个意图来打开 BluetoothLeGatt 的第一个 activity(即 DeviceScanActivity.java)单击我也在新的 xml 文件中创建的按钮。我将这个新的 activity 作为启动器放在清单文件中,并将下面的 DeviceScanActivity 放在清单中。在编译过程中,没有错误但是当我在 phone 上打开 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.access0(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.access0(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

这是 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),这将更改您的操作栏的外观,并为您的所有活动扩展 AppCompatActivity or 扩展 Activity 在你的 TestLaunchActivity 中。 AppCompatActivity 用于支持旧版本的 Android APIs

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

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