[英]Android Studio 3.6.1 error when i hook frame layout with autocomplete
Hello I just want to switch from a fragment to another using a button.您好,我只想使用按钮从一个片段切换到另一个片段。 Nothing difficult... the code works well but I have spent a lot of time because Android goesto error if I hook frameLayout with autocompile.
没什么难的......代码运行良好,但我花了很多时间,因为如果我用自动编译挂钩 frameLayout,Android 会出错。 I'm explaining better:
我解释得更好:
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();
}
In all rows where you see R.id.frameLayout i have to handly write frameLayout..... Android colors it red but It works...在您看到 R.id.frameLayout 的所有行中,我必须手动编写 frameLayout ..... Android colors 它是红色的,但它可以工作......
But if i choose the resource with autocompile...但是,如果我选择具有自动编译功能的资源...
...when i run app Android gives me this error.... ...当我运行应用程序 Android 给我这个错误....
Why?为什么? is not best practice use autocompile?
不是最佳实践使用自动编译吗? Thanks in advance
提前致谢
Try this may will help you.Go to "File" -> "Invalidate Caches...", and select "Invalidate and Restart" option to fix this.试试这个可能会对你有所帮助。Go 到“文件”->“无效缓存...”,以及 select“无效并重新启动”选项来解决这个问题。
Or或者
Restar you android studio and emulator重新启动你 android 工作室和仿真器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.