-1

I'm trying to launch a fragment from an activity. However, when I run the app and click on the button that has to launch the fragment I get the error:

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

I can't see where I've made something wrong. It should be correct. This is the class where I implement the fragment. I'm only gonna upload the View onCreateView for this class as no more is needed:

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

I launch the fragment from here:

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

What have I missed?

Any help is appreciated!

EDIT: This is the XML for the activity:

<?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>

And this is the 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>

And this is the 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>
Hudhud
  • 61
  • 1
  • 10

2 Answers2

0

My mistake was that I created the fragment as a separate layout and not as a framelayout on the frontpage. So the solution was to create a framelayout on the frontpage. That makes sense :)

Hudhud
  • 61
  • 1
  • 10
0

First to the layout of activity, add this

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

Then in that activity java code add

        Fragment fragment = new Kontakt();
        getFragmentManager().beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
Kashif Anwaar
  • 728
  • 7
  • 14