java - fragment 交换时应用程序停止工作 - Java Android

标签 java android androidx

我试图在我的应用中放置 fragment ,它甚至可以正常工作,但是当通过BottomNavigationView切换屏幕时,应用程序停止运行

java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/lifecycle/MutableLiveData;

我尝试过不同的 Lifecycle 实现,但根本没有帮助


android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "*************"
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0-rc04"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
  implementation 'androidx.appcompat:appcompat:1.0.0'
  implementation 'androidx.legacy:legacy-support-v4:1.0.0'
  implementation 'com.google.android.material:material:1.0.0-rc01'
  implementation 'androidx.cardview:cardview:1.0.0'
  implementation 'androidx.annotation:annotation:1.1.0'
  testCompile 'junit:junit:+'
  compile 'com.wang.avi:library:2.1.3'
}

--------------
Fragment used
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;

public class Lessons extends Fragment
{

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View inflate = inflater.inflate(R.layout.lessons_fragment, container, false);
        return inflate;
    }
}

最佳答案

您需要定义 android 生命周期依赖项才能使用 LiveData。

详情请参阅以下页面:https://developer.android.com/jetpack/androidx/releases/lifecycle

对于当前版本,将以下内容添加到您的依赖项中:

def lifecycle_version = "2.1.0"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

如果您实际上没有使用任何 LiveData 组件,那么发布完整的堆栈跟踪会更有用。

关于java - fragment 交换时应用程序停止工作 - Java Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57892614/

相关文章:

java - 为什么缺少授权 header ?

java - 检查 startTime 和 endTimer 是否在两个时间之间

java - 使用 CaesarCipherBreaker 拆分字符串

java - 为什么Androidx.appcompat.AppCompatActivity抛出java.lang.NoSuchMethodError : No virtual method getMainExecutor()?

androidx - Android - 导入 ViewPager2 库时程序类型已存在 com.transition.R

java - 最近迁移到 androidx,我发现 alertdialog 的布局问题

java - 为什么 InvocableTargetException 被设计为将其原因存储为目标?

android - react-native - 在 css StyleSheet 中添加公式会影响性能吗?

android - Google 使用 WebView 登录应用程序

android - 无法在 okhttp3 Android 中访问 POST 参数