Как добавить активность до примера «BluetoothLeGatt»? - PullRequest
0 голосов
/ 18 февраля 2019

Как новичок в программировании, я взял пример кода Android Studio «BluetoothLeGatt», и весь проект работает как надо.Однако, когда я пытаюсь добавить действие (я добавил java-файл, в котором правильно использую намерение открыть первое действие BluetoothLeGatt (то есть DeviceScanActivity.java) после нажатия на кнопку, которую я создаю также в новом xml-файле.Я поместил это новое действие в качестве начального в файл манифеста и поместил 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 голосов
/ 19 февраля 2019

Вот важная часть из 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.

Здесь есть больше ресурсов по этому вопросу - Вам нужно использовать тему Theme.AppCompat (или потомок) с этим действием

Кроме того, logcat имеет несколько уровней важности для журналов.Вы можете использовать фильтры (debug/info/error и т. Д.) В logcat.Каждый раз, когда происходит сбой приложения, вы увидите stacktrace с ошибками.Также ошибки печатаются красным цветом, поэтому их легко идентифицировать.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...