【问题标题】:Why isn't the view found for the id?为什么找不到该 id 的视图?
【发布时间】:2016-08-09 00:21:50
【问题描述】:

我正在尝试从活动中启动片段。但是,当我运行应用程序并单击必须启动片段的按钮时,我收到错误:

java.lang.IllegalArgumentException: No view found for id 0x7f0e0074 (com.example.hudhud.islam:id/kontaktfragment) for fragment Kontakt{aaaed67 #1 id=0x7f0e0074}

我看不出我哪里做错了。应该是正确的。这是我实现片段的类。我只会为这个课程上传View onCreateView,因为不再需要了:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_kontakt, container, false);

        sendmail = (Button) view.findViewById(R.id.sendknap);
        sendmail.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                msg = (EditText) view.findViewById(R.id.besked);
                String message = msg.getText().toString();
                sendemail(message);
            }
        });

        return view;
    }

我从这里启动片段:

   @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.Kontakt) {
            Fragment fragment = new Kontakt();
            getFragmentManager().beginTransaction()
                    .add(R.id.kontaktfragment, fragment)
                    .commit();
        }
        return super.onOptionsItemSelected(item);
    }

我错过了什么?

感谢任何帮助!

编辑: 这是活动的 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#32c6a6"
    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:id="@+id/velkomst"
        android:textSize="20dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_margin="10dp" />

    <FrameLayout
        android:id="@+id/Buttons"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_below="@id/velkomst" >

    </FrameLayout>

</RelativeLayout>

这是 kontakfrag XML:

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/kontaktfragment"></FrameLayout>
</RelativeLayout>

这是 fragment_kontakt XML:

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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/besked"
        android:layout_weight="0.70"
        android:textSize="20dp"
        android:layout_marginTop="30dp"
        android:textIsSelectable="true"
        android:textColor="#000000"
        android:background="#ffffff"
        android:gravity="top"
        android:paddingTop="30dp"
        android:hint="Hvis du har feedback eller nogle spørgsmål, så er du velkommen til at skrive. Vi besvarer mailen hurtigst muligt"
        android:scrollIndicators="right"/>

    <Button
        android:layout_width="144dp"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/sendknap"
        android:layout_gravity="center_horizontal"
        android:layout_weight="0.05"
        android:textSize="20dp"/>
</LinearLayout>

【问题讨论】:

  • Exception 消息告诉您,在 Activity 的布局中找不到 ID 为 kontaktfragmentViewGroup
  • 为什么会这样?我已经制作了片段并称之为 kontaktfragment
  • 我不明白你的意思,但是你在add()调用中作为第一个参数传递的ID必须是ActivityViewGroup的ID。您没有提供足够的信息让我们能够确定您到底错在哪里。
  • 您还需要什么?我尝试只上传相关的内容。
  • kontakfrag XML在代码中的作用是什么,我好像不太明白它的位置。

标签: android android-fragments android-activity fragmentmanager


【解决方案1】:

我的错误是我将片段创建为单独的布局,而不是 frontpage 上的框架布局。所以解决方案是在frontpage 上创建一个框架布局。这是有道理的:)

【讨论】:

    【解决方案2】:

    先到activity的布局,添加这个

    <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    

    然后在那个活动中添加java代码

            Fragment fragment = new Kontakt();
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();
    

    【讨论】:

      猜你喜欢
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 2013-12-20
      相关资源
      最近更新 更多