【问题标题】:Cannot resolve symbol 'LocalBroadcastManager'无法解析符号“LocalBroadcastManager”
【发布时间】:2019-07-19 14:49:43
【问题描述】:

我关注了一个 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(模块:app):

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'
    }

蓝牙连接服务器.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”

【问题讨论】:

  • 只在android组件类中可用
  • 我怎样才能将这个类/库添加到我的项目中?

标签: android libraries


【解决方案1】:

将此添加到您的app/build.gradle 文件中,在dependencies 内:

implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'

然后重建项目

这应该可以工作

【讨论】:

  • 谢谢!它现在正在工作。如果其他人有同样的问题,你必须将“ implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' ”放在依赖项下。
  • 是的..我也忘了提到implementation 部分
  • 谢谢,它正在工作。实际上这应该默认添加到 androidx.appcompat:appcompat:1.1.0 库中,为什么在 AndroidX 中对 localbroadcastmanager 有特殊依赖。
猜你喜欢
  • 2020-01-28
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 2016-12-31
  • 1970-01-01
相关资源
最近更新 更多