0

As a newbie to programming, I took the Android Studio example code of "BluetoothLeGatt" and the whole project itself works as it should. However, when I try to add an activity (I added a java file where I correctly use an intent to open the first activity of BluetoothLeGatt (which is DeviceScanActivity.java) after clicking on a button which I create also in a new xml file. I put this new activity as starter within the manifest file and put the DeviceScanActivity below in the manifest. During compilation, there is no error but when I open the apk on my phone, the app crashes immediately. What do I have to do differently?

TestLaunchActivity.java (the new one which should go first)

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

MMeier
  • 43
  • 4
  • You can see the error in `logcat`. Post `logcat` messages here. – Ranjan Feb 18 '19 at 06:57
  • Thanks for your swift reply. Since the app uses Bluetooth, it does not run on my notebook but only on my phone. Therefore, I don't have a logcat so far... – MMeier Feb 18 '19 at 12:04
  • Even when your app is running on phone and not emulator you can get `logcat` in `Android Studio`. How are you running the app on phone? via USB? – Ranjan Feb 18 '19 at 13:30
  • I build the apk file and copy/paste it manually to my phone via usual USB cable. – MMeier Feb 18 '19 at 13:58
  • Why don't you run it in debug mode? There is a lot of info here https://developer.android.com/studio/debug/. In short `1.` Unlock `developer mode` on phone `2.` Enable `USB debugging` `3.` Attach phone via USB `4.` `Run Debug 'app'` on Android studio while the phone is attached – Ranjan Feb 18 '19 at 16:25
  • Thanks for the hint. I added the logcat above but cant make much sense out of it myself...it is added with backsticks but also doesn't seem very readable, sorry for that... – MMeier Feb 18 '19 at 19:24
  • Your `TestLaunchActivity` should **extends** `Activity` not `AppCompatActivity`. – Tommy Feb 19 '19 at 03:09
  • ok, great, that solved the problem, many thanks! – MMeier Feb 19 '19 at 18:56

1 Answers1

0

Here is the important part from the 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.

You are using Theme.Holo.Light for your application but your TestLaunchActivity extends AppCompatActivity which inherits Theme.AppCompat. Either change your application theme to Theme.AppCompat and its descendant themes (Light/Dark) which changes how your action bar looks like and also extend AppCompatActivity for all of your activities or extend Activity in your TestLaunchActivity. AppCompatActivity is used to support older versions of Android APIs.

Here are more resources on this - You need to use a Theme.AppCompat theme (or descendant) with this activity

Besides this, logcat has multiple level of importance for logs. You can use filters(debug/info/error etc) in logcat. Whenever app crashes, you'll see relevant stacktrace in errors. Also errors are printed in red color so it's easy to identify them.

Ranjan
  • 1,096
  • 10
  • 22