RuntimeException:无法启动活动componentInfo和InflateException:从setcontentview()扩展类碎片的错误

时间:2014-11-17 01:50:24

标签: android xml setcontentview

我目前正在编写一个使用多个片段的简单Android应用程序。我一直收到运行时错误,上面的消息指向我的setContentView()方法。 这是代码:

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

}

我不确定为什么但是logcat指向setContentView()。此外,还有一个与此主要活动一起使用的.xml文件。我不确定它是否会有所帮助。

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

我很抱歉,因为我不知道如何在这里包含logcat ..请问我是否需要其他任何东西。我没有包含所有文件,因为我不认为它们是问题的原因,而且可能太多了。我真的希望有人可以帮助我。

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 个答案:

答案 0 :(得分:1)

Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText 

这是重要的部分。 Logcat在开始时看起来很难阅读,但它几乎总能告诉你什么是错的。

下一行甚至会告诉你这种情况发生的地方:

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

错误不在您的“活动”中,但在您的工具栏内,您尝试将Button分配给EditText,可能会混淆一些看似

EditText someEditText = (Button) view.findViewByid(R.id.button1); 
第50行@t ToolbarFragment