片段集合中的 Android XML ID '@android:id'无法解决符号错误。



我是新来的。 我有一个用于片段集合对象的 XML。 在我有两个完美工作的文本视图。 当我尝试以完全相同的格式添加另一个接收无法解决错误时。(即:android:id="@android:id/tvNewText")。如果我像常规XML一样添加一个id(即:android:id="@+id/tvAnotherNewText"),我无法从我的Fragment onCreateView访问它。 有什么帮助吗?fragment_collection_object.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="40sp"
    android:padding="32dp"
    android:text="Sports Bar" />

这工作正常,但直接在它下面,我添加了一个新的文本视图,它无法解析 id。

 <TextView
    android:id="@android:id/tvNewText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Some Words" />

尝试从下面的代码访问:

 public static class DemoObjectFragment extends Fragment {
    public static final String ARG_OBJECT = "object";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(android.R.id.text1)).setText((args.getString(ARG_OBJECT)));
        ((TextView) rootView.findViewById(android.R.id.tvNewText)).setText("heyo");
        return rootView;
    }
}

在这里,text1 可以正常工作且符合预期,但无法在 tvNewText 上解析。 提前感谢任何帮助。

使用 android:id="@+id/tvNewText" 解决错误。

最新更新