Closed. This question is
not reproducible or was caused by typos。它当前不接受答案。
想改善这个问题吗?
Update the question,所以它是
on-topic,用于堆栈溢出。
21天前关闭。
我做了什么 :
我正在尝试将具有相对布局的.XML添加到名为“ home_frag”的片段中。
我首先按照如下所述在布局中创建了home_frag.XML,然后创建了下面提到的片段Java类。
但是,当我尝试在fragment类中填充home_frag.XML时,它说“无法解析符号'home_frag'”,并且如下所述是红色的。
似乎我的.XML在片段类内部不可访问。
我尝试了Android Studio建议的3个选项
*重命名-没用,因为它建议其他布局。
*创建布局资源值-尝试在strings.xml内创建字符串“ home_frag”,但无法解决问题。
*创建布局资源文件-我删除了'home_frag',然后使用此选项再次创建。但是,当我尝试将其充气成红色时,结果是相同的,这意味着无法访问布局文件。
我期望的是:
这有点奇怪,我不知道发生了什么。我也可以在布局文件中看到“ home_frag”。
我要寻找的只是将“ home_frag.xml”与“ HomeFrag.java”片段类连接在一起,该片段类始终显示为红色。
HomeFrag.java:
package com.example.myapplication.fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
public class HomeFrag extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_frag,container,false);
}
}
home_frag.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8BC34A">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:text="@string/notifications"
android:textSize="30sp"
android:layout_centerInParent="true"
/>
</RelativeLayout>
我对此表示感谢。我正在使用Android Studio 3.6.1。
谢谢。
答:
我只需执行
File > Invalidate Chaches / Restart.
就可以解决我的问题