升级我的 Fragment 的导航版本时出现问题(从 2.3.5 到 2.4.0-alpha03)

时间:2023-01-14
本文介绍了升级我的 Fragment 的导航版本时出现问题(从 2.3.5 到 2.4.0-alpha03)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我一直在尝试将片段的导航版本从 2.3.5 升级到 2.4.0-alpha03,以便它可以根据文档支持多个回栈https://developer.android.com/jetpack/androidx/releases/navigation

I have been trying to upgrade my fragment's navigation version from 2.3.5 to 2.4.0-alpha03 so that it can support multiple back stacks as per the documentation https://developer.android.com/jetpack/androidx/releases/navigation

并且还帮助保存和恢复我的每个底部导航项目的状态几天现在不成功.每次我在 build.gradle 中同步实现时,我的 homeactivity 中的代码都会抛出此错误 cannot resolve symbol

and also help in saving and restoring the state of each of my bottom nav items for a few days now unsuccessfully. Each time I sync the implementations in my build.gradle, code in my homeactivity throws this error cannot resolve symbol

final NavController navController = navHostFragment.getNavController(); 
NavigationUI.setupWithNavController(bottomNavigationView, navController);
FirstFragment firstFragment = (FirstFragment) navHostFragment.getChildFragmentManager().getFragments().get(0);

以及他们的进口

import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

也抛出错误未使用导入语句,无法解析符号.因此,如果我运行该应用程序,它就无法执行我希望它执行的上述功能.

throw an error too unused import statement, cannot resolve symbol. So if I run the app, it's unable to perform the above functions that I want it to.

这是我迄今为止尝试过的:

Here's what i've tried so far:

  • 我添加了这个实现androidx.navigation:navigation-fragment:2.4.0-alpha03"并实现androidx.navigation:navigation-ui:2.4.0-alpha03"到我的build.gradle(app).
  • I've added this implementation "androidx.navigation:navigation-fragment:2.4.0-alpha03" and implementation "androidx.navigation:navigation-ui:2.4.0-alpha03" to my build.gradle(app).

但它并没有解决.

编辑

我的 Android Gradle 插件版本是 4.0.2

My Android Gradle Plugin version is 4.0.2

我的 Gradle 版本是 6.1.1

My Gradle Version is 6.1.1

我相信有些事情我没有做对,或者我应该做的事情我没有做.如果有人能指出这一点,我将不胜感激.

I believe there's something I didn't do right or something I'm meant to do that I didn't do. Please I'd appreciate it if anyone can identify that.

这是我的具体代码:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.viz.lightweatherforecast"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation "androidx.navigation:navigation-fragment:2.4.0-alpha03"
    implementation "androidx.navigation:navigation-ui:2.4.0-alpha03"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.media2:media2:1.0.0-alpha04'
    implementation 'org.jetbrains:annotations:15.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

Build.gradle(项目):

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.2"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

HomeActivity.java:

package com.viz.lightweatherforecast.Activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.viz.lightweatherforecast.FirstFragment;
import com.viz.lightweatherforecast.R;
import com.viz.lightweatherforecast.Retrofit.ApiClient;
import com.viz.lightweatherforecast.Retrofit.ApiInterface;
import com.viz.lightweatherforecast.Retrofit.Example;

import org.jetbrains.annotations.NotNull;

import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeActivity extends AppCompatActivity {
    // Last update time, click sound, search button, search panel.
    TextView time_field;
    MediaPlayer player;
    ImageView Search;
    EditText textfield;
    // For scheduling background image change(using constraint layout, start counting from dubai, down to statue of liberty.
    ConstraintLayout constraintLayout;
    public static int count=0;
    int[] drawable =new int[]{R.drawable.dubai,R.drawable.central_bank_of_nigeria,R.drawable.eiffel_tower,R.drawable.hong_kong,R.drawable.statue_of_liberty};
    Timer _t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        // use home activity layout.

        time_field = findViewById(R.id.textView9);
        Search = findViewById(R.id.imageView4);
        textfield = findViewById(R.id.textfield);
        //  find the id's of specific variables.

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        // host 3 fragments along with bottom navigation.
        final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
        assert navHostFragment != null;
        final NavController navController = navHostFragment.getNavController();
        NavigationUI.setupWithNavController(bottomNavigationView, navController);


        // For scheduling background image change
        constraintLayout = findViewById(R.id.layout);
        constraintLayout.setBackgroundResource(R.drawable.dubai);
        _t = new Timer();
        _t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                // run on ui thread
                runOnUiThread(() -> {
                    if (count < drawable.length) {

                        constraintLayout.setBackgroundResource(drawable[count]);
                        count = (count + 1) % drawable.length;
                    }
                });
            }
        }, 5000, 5000);

        Search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // make click sound when search button is clicked.
                player = MediaPlayer.create(HomeActivity.this, R.raw.click);
                player.start();

                getWeatherData(textfield.getText().toString().trim());
                // make use of some fragment's data
                FirstFragment firstFragment = (FirstFragment) navHostFragment.getChildFragmentManager().getFragments().get(0);
                firstFragment.getWeatherData(textfield.getText().toString().trim());

            }

            private void getWeatherData(String name) {

                ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);

                Call<Example> call = apiInterface.getWeatherData(name);

                call.enqueue(new Callback<Example>() {
                    @Override
                    public void onResponse(@NonNull Call<Example> call, @NonNull Response<Example> response) {

                        try {
                            assert response.body() != null;
                            time_field.setVisibility(View.VISIBLE);
                            time_field.setText("Last Updated:" + " " + response.body().getDt());
                        } catch (Exception e) {
                            time_field.setVisibility(View.GONE);
                            time_field.setText("Last Updated: Unknown");
                            Log.e("TAG", "No City found");
                            Toast.makeText(HomeActivity.this, "No City found", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(@NotNull Call<Example> call, @NotNull Throwable t) {
                        t.printStackTrace();
                    }

                });
            }

        });
    }
}

Activity_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout"
    android:background="@drawable/dubai"
    tools:context=".Activity.HomeActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_menu" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="409dp"
        android:layout_height="599dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:navGraph="@navigation/my_nav"
        />

    <EditText
        android:id="@+id/textfield"
        android:layout_width="250dp"
        android:layout_height="35dp"
        android:autofillHints="@string/change_city"
        android:background="@color/colorPrimary"
        android:hint="@string/search_city"
        android:inputType="text"
        android:labelFor="@id/imageView4"
        android:padding="8dp"
        android:textColor="@color/colorAccent"
        android:textSize="16sp"
        app:layout_constraintEnd_toStartOf="@+id/imageView4"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="50dp"
        android:layout_height="35dp"
        android:layout_marginEnd="1dp"
        android:contentDescription="@string/searchbtn"
        android:src="@drawable/look"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/time_field"
        android:visibility="gone"
        android:textColor="#FFFFFF"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textfield" />


</androidx.constraintlayout.widget.ConstraintLayout>

推荐答案

我也在做同样的事情 首先不要使用 2.4.0 alpha-03 因为他们有问题.

I am also working on the same thing first of all don't use 2.4.0 alpha-03 because they have a problem .

从开发者页面复制粘贴

已知问题

在使用 Gradle 6.7.0 时,由于依赖于旧版本的 Kotlin.这可以通过更新使用 Gradle 7.0 来解决.(b/190739257)

Safe Args will fail with an Unable to find method ‘’java.lang.String kotlin.text.CarsKt.titleCase(char, java.util.Locale)’’ error when using Gradle 6.7.0 due to a dependency on an older version of Kotlin. This can be worked around by updating to use Gradle 7.0. (b/190739257)

所以这将在 2.4.0 alpha04 中修复,然后使用 2.4.0 alpha02

So this will fix in 2.4.0 alpha04 till then use 2.4.0 alpha02

所以这不是实际问题尝试降级并使用最新日志更新您的问题,以便我可以帮助您

So this is not the actual problem try to downgrade and update your question with the latest logs so I can help you that

这篇关于升级我的 Fragment 的导航版本时出现问题(从 2.3.5 到 2.4.0-alpha03)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何使用 Selenium 在网页上单击打印按钮 下一篇:Vaadin(流):使用共享对象导航到目的地

相关文章

最新文章