0

我目前正在编写一个使用多个片段的简单 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
4

1 回答 1

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) 

错误不在您的活动中,而是在您的 ToolbarFragment 中,您尝试将 a 分配给 a ButtonEditText可能在一行中混淆了一些东西

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

在第 50 行 @你的 ToolbarFragment。

于 2014-11-17T02:00:51.583 回答