Android Studio 3.6.1 当我用自动完成功能钩住框架布局时出现错误。

问题描述 投票:0回答:1

你好,我只是想从一个片段切换到另一个使用按钮。没什么难的......代码工作良好,但我花了很多时间,因为Android去错误,如果我挂钩frameLayout与自动编译.我解释更好。

package com.example.fragcookbook;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

    FragmentOne mFragmentOne;
    FragmentTwo mFragmentTwo;
    int showingFragment=0;

    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        mFragmentOne = new FragmentOne();
        mFragmentTwo = new FragmentTwo();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.frameLayout, mFragmentOne);
        fragmentTransaction.commit();
        showingFragment=1;

    }
    public void switchFragment(View view) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (showingFragment==1) {
            fragmentTransaction.replace(R.id.frameLayout, mFragmentTwo);
            showingFragment = 2;
        } else {
            fragmentTransaction.replace(R.id.frameLayout, mFragmentOne);
            showingFragment=1;
        }
        fragmentTransaction.commit();
    }

在所有你看到R.id.frameLayout的行中,我必须手动写出frameLayout......。安卓把它染成了红色,但它是有效的......。enter image description here

但是如果我选择自动编译的资源......当我运行应用时,Android系统就会将其显示为红色......。enter image description here

...当我运行应用程序时,Android给我这个错误......为什么?enter image description here

为什么呢,是不是最好的做法使用自动编译?

android android-studio android-fragments autocomplete android-framelayout
1个回答
1
投票

试试这个可能会帮助你.进入 "文件" -> "Invalidate Caches...",并选择 "Invalidate and Restart "选项来解决这个问题。

试试这个可能会有帮助.进入 "文件"-> "无效缓存...",选择 "无效并重新启动 "选项来解决这个问题。

重新启动你的安卓工作室和模拟器

© www.soinside.com 2019 - 2024. All rights reserved.