0

I know there are many questions on this, but I can not get my code working following them. Here is my main activity;

import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements TextFragment.OnFragmentInteractionListener{
@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        if (savedInstanceState==null){
        TextFragment textFragment=new TextFragment();
        textFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container,textFragment).commit();
    }
}
public void onFragmentInteraction(Uri uri){
    Toast toast=Toast.makeText(this,"Clicked",Toast.LENGTH_SHORT);
    toast.show();
}
}

Here is the XML for the fragment;

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.me.fragmenttest.TextFragment">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Question"
    android:id="@+id/titleText"
    android:layout_gravity="center_horizontal|top" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_gravity="center" />

</FrameLayout>

If I run this this throws a fatal exception saying;

java.lang.IllegalArgumentException: No view found for id 0x7f0b0058

According to the advice hereI changed;

setContentView(R.layout.activity_main);

to

setContentView(R.layout.fragment_text);

app runs without error, but I do not get the toast. How can I eliminate this error? Thank you.

Community
  • 1
  • 1
manjulapra
  • 85
  • 1
  • 2
  • 9
  • You are importing the correct one ? – Lucas Ferraz Aug 02 '16 at 20:32
  • @LucasPaolillo. Thanks. Which one do you think is wrong? Actually I think this is related to the fact that fragment is not a child o fthe main activity in layout. But I am not sure how to correct that! – manjulapra Aug 04 '16 at 14:24

0 Answers0