I am currently writing a simple android app that makes use of multiple fragments. I keep getting a runtime error with the above message that points toward my setContentView() method. Here is the code:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;


public class FragmentExampleActivity extends FragmentActivity implements ToolbarFragment.ToolbarListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment_example);

}

@Override
public void onButtonClick(int fontsize, String text)
{
    //instantiates the new fragments as well as start editing the text
    TextFragment textFragment = (TextFragment) getSupportFragmentManager()
            .findFragmentById(R.id.text_fragment);
    textFragment.changeTextProperties(fontsize, text);

    OppositeFragment oppositeFragment=(OppositeFragment) getSupportFragmentManager()
            .findFragmentById(R.id.opposite_fragment);
    oppositeFragment.changeTextProperties(fontsize, text);      
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.fragment_example, menu);
    return true;
}

}

I am not sure why but the logcat points to the setContentView(). Also, there is an .xml file that goes along with this main activity. I am not sure if it will help or not.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentExampleActivity" >

    <fragment
        android:id="@+id/toolbar_fragment"
        android:name="meng.jerry.fragmentexample.ToolbarFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        tools:layout="@layout/toolbar_fragment"/>

    <fragment
        android:id="@+id/text_fragment"
        android:name="meng.jerry.fragmentexample.TextFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/toolbar_fragment"
        android:layout_marginTop="40dp"
        tools:layout="@layout/text_fragment" />

    <fragment
        android:id="@+id/opposite_fragment"
        android:name="meng.jerry.fragmentexample.OppositeFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/text_fragment"
        android:layout_marginTop="16dp"
        tools:layout="@layout/opposite_fragment" />

</RelativeLayout>

I apologize because I have no idea how to include logcat here.. Please ask me if anything else is needed. I have not included every file because I don't think they are the cause of the problem and it may be too much. I really hope someone can help me.

11-16 20:27:04.601: E/AndroidRuntime(1309): FATAL EXCEPTION: main
11-16 20:27:04.601: E/AndroidRuntime(1309): Process: meng.jerry.fragmentexample, PID: 1309
11-16 20:27:04.601: E/AndroidRuntime(1309): java.lang.RuntimeException: Unable to start activity ComponentInfo{meng.jerry.fragmentexample/meng.jerry.fragmentexample.FragmentExampleActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.os.Looper.loop(Looper.java:136)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at java.lang.reflect.Method.invoke(Method.java:515)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at dalvik.system.NativeStart.main(Native Method)
11-16 20:27:04.601: E/AndroidRuntime(1309): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.app.Activity.setContentView(Activity.java:1929)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at meng.jerry.fragmentexample.FragmentExampleActivity.onCreate(FragmentExampleActivity.java:19)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.app.Activity.performCreate(Activity.java:5231)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-16 20:27:04.601: E/AndroidRuntime(1309):     ... 11 more
11-16 20:27:04.601: E/AndroidRuntime(1309): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
11-16 20:27:04.601: E/AndroidRuntime(1309):     at meng.jerry.fragmentexample.ToolbarFragment.onCreateView(ToolbarFragment.java:50)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1099)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1201)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:292)
11-16 20:27:04.601: E/AndroidRuntime(1309):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
11-16 20:27:04.601: E/AndroidRuntime(1309):     ... 21 more
1

There are 1 answers

2
fweigl On BEST ANSWER
Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText 

This is the important part. The Logcat can seem difficult to read in the beginning, but it almost always tells you what's wrong.

The next line even tells you where this happens:

E/AndroidRuntime(1309): at meng.jerry.fragmentexample.ToolbarFragment.onCreateView(ToolbarFragment.java:50) 

The error is not in your Activity but in your ToolbarFragment, you're trying to assign a Button to an EditText, probably mixed up something in a line that looks like

EditText someEditText = (Button) view.findViewByid(R.id.button1); 

in line 50 @ your ToolbarFragment.