为什么找不到id的视图?

时间:2016-08-09 00:21:51

标签: android android-fragments android-activity fragmentmanager

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

    def test(self, obj):
        current_bucket = obj.bucket
        current_key = obj.key
        client = boto3.client('s3', aws_access_key_id=settings.AWS_ACCESS_ID, aws_secret_access_key=settings.AWS_SECRET_KEY)
        client.download_file(current_bucket, current_key, "temp.file")
        # do the file processing
        # delete the temp.file

我看不出我做错了什么。应该是正确的。这是我实现片段的类。我只会为此课程上传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;
    }

我错过了什么?

感谢任何帮助!

编辑: 这是活动的XML:

   @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);
    }

这是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: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>

这是fragment_kontakt 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>

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

首先进行活动布局,添加此

componentWillReceiveProps

然后在该活动中添加java代码

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