无法解析符号“ LocalBroadcastManager”

时间:2019-07-19 14:49:43

标签: android libraries

我遵循了YouTube教程(https://www.youtube.com/watch?v=y8R2C86BIUc&list=PLgCYzUzKIBE8KHMzpp6JITZ2JxTgWqDH2),并且在最后一步中,教师使用了名为LocalBroadcastManager的类。但是不再支持此类。.(https://developer.android.com/reference/androidx/localbroadcastmanager/content/LocalBroadcastManager)并且由于此类/错误,Android Studio无法编译。 据我了解-我是Android Studio / Java的新手-有一个选项可以将项目迁移到androidx-libraries以使代码运行,但是我不知道该怎么做。

如果我要问一个绝对新手的问题,请给我一个提示,在这里我可以获取解决问题的基本信息。 另外,Android Studio的更改/开发速度非常快,因此据我所知,那里的解决方案不再是最新的。

我尝试了stackoverflow的一些解决方案,但由于是绝对的初学者,所以我无法弄清楚该放在哪里。

build.gradle(模块:应用):

apply plugin: 'com.android.application'


android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.btytcodingwithmitch"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
    testImplementation 'junit:junit:4.13-beta-3'
    androidTestImplementation 'androidx.test:runner:1.3.0-alpha01'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha01'
    }

BluetoothConnectionServer.java:

package com.example.btytcodingwithmitch;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.security.PrivateKey;
import java.util.UUID;

public class BluetoothConnectionService {

  ...

   private class ConnectedThread extends Thread{

        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;


        public void run(){
            byte[] buffer = new byte[1024]; //buffer store for stream

            int bytes; //bytes returned from read

            while (true){
                //read from Input Stream
                try {
                    bytes = mmInStream.read(buffer);
                    String incomingMessage = new String(buffer, 0, bytes);
                    Log.d(TAG, "InputStream" + incomingMessage);

                    Intent incomingMessageIntent = new Intent("incoming Message");
                    incomingMessageIntent.putExtra("theMessage", incomingMessage);

                    LocalBroadcastManager.getInstance(mContext).sendBroadcast(incomingMessageIntent);

                } catch (IOException e) {
                    Log.e(TAG, "write: Error reading inputstream" + e.getMessage());
                    break;
                }
            }
        }

错误消息

  

“错误:找不到符号变量LocalBroadcastManager”

1 个答案:

答案 0 :(得分:1)

将此添加到您的build.gradle文件中:

androidx.localbroadcastmanager:localbroadcastmanager:1.0.0

然后重建项目

这应该有效