无法使用片段加载活动

时间:2013-11-26 12:25:38

标签: java android xml android-fragments android-activity

这是我第一次使用Fragments。看看下面的代码。

activity_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <fragment 
        android:id="@+id/firstViewFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.example.fragmenttest.FirstView"/>

    <fragment 
        android:id="@+id/secondViewFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.example.fragmenttest.SecondView"/>

</RelativeLayout>

fragment_view1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/viewOneText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="92dp"
        android:layout_marginTop="182dp"
        android:text="First View"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/viewOneBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp"
        android:text="Click Here" />

</RelativeLayout>

fragment_view2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffddff" >

    <TextView
        android:id="@+id/secondViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="190dp"
        android:text="View 2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/secondViewBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/secondViewText"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

MainActivity.java

package com.example.fragmenttest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

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

}

FirstView.java

package com.example.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FirstView extends Fragment
{
    private TextView firstText;
    private Button btn;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        firstText = (TextView)getView().findViewById(R.id.viewOneText);
        btn = (Button)getView().findViewById(R.id.viewOneBtn);

        btn.setOnClickListener(new ButtonEvent());

        View view = inflater.inflate(R.layout.fragment_view1,container,false);
        return view;

    }

    private class ButtonEvent implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            firstText.setText("First View Btn Clicked");

        }

    }
}

SecondView.java

package com.example.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class SecondView extends Fragment
{
    private TextView secondText;
    private Button secondViewBtn;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        secondText = (TextView)getView().findViewById(R.id.secondViewText);
        secondViewBtn = (Button)getView().findViewById(R.id.secondViewBtn);

        secondViewBtn.setOnClickListener(new ButtonEvent());

        View view = inflater.inflate(R.layout.fragment_view2,container,false);
        return view;

    }

    private class ButtonEvent implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            secondText.setText("Second View Text changed");

        }

    }
}

当我运行此应用程序时,我收到以下错误

11-26 12:18:36.137: E/AndroidRuntime(850): FATAL EXCEPTION: main
11-26 12:18:36.137: E/AndroidRuntime(850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmenttest/com.example.fragmenttest.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.os.Looper.loop(Looper.java:137)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread.main(ActivityThread.java:5041)
11-26 12:18:36.137: E/AndroidRuntime(850):  at java.lang.reflect.Method.invokeNative(Native Method)
11-26 12:18:36.137: E/AndroidRuntime(850):  at java.lang.reflect.Method.invoke(Method.java:511)
11-26 12:18:36.137: E/AndroidRuntime(850):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-26 12:18:36.137: E/AndroidRuntime(850):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-26 12:18:36.137: E/AndroidRuntime(850):  at dalvik.system.NativeStart.main(Native Method)
11-26 12:18:36.137: E/AndroidRuntime(850): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-26 12:18:36.137: E/AndroidRuntime(850):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Activity.setContentView(Activity.java:1881)
11-26 12:18:36.137: E/AndroidRuntime(850):  at com.example.fragmenttest.MainActivity.onCreate(MainActivity.java:12)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Activity.performCreate(Activity.java:5104)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-26 12:18:36.137: E/AndroidRuntime(850):  ... 11 more
11-26 12:18:36.137: E/AndroidRuntime(850): Caused by: java.lang.ClassCastException: com.example.fragmenttest.FirstView cannot be cast to android.app.Fragment
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Fragment.instantiate(Fragment.java:585)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Fragment.instantiate(Fragment.java:560)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.app.Activity.onCreateView(Activity.java:4709)
11-26 12:18:36.137: E/AndroidRuntime(850):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
11-26 12:18:36.137: E/AndroidRuntime(850):  ... 21 more

为什么我无法运行?

更新

正如SO用户所说,我改变了我的MainActivity

package com.example.fragmenttest;

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

public class MainActivity extends FragmentActivity {

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

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

}

现在我有以下错误

11-26 12:35:34.137: E/AndroidRuntime(968): FATAL EXCEPTION: main
11-26 12:35:34.137: E/AndroidRuntime(968): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmenttest/com.example.fragmenttest.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.os.Looper.loop(Looper.java:137)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread.main(ActivityThread.java:5041)
11-26 12:35:34.137: E/AndroidRuntime(968):  at java.lang.reflect.Method.invokeNative(Native Method)
11-26 12:35:34.137: E/AndroidRuntime(968):  at java.lang.reflect.Method.invoke(Method.java:511)
11-26 12:35:34.137: E/AndroidRuntime(968):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-26 12:35:34.137: E/AndroidRuntime(968):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-26 12:35:34.137: E/AndroidRuntime(968):  at dalvik.system.NativeStart.main(Native Method)
11-26 12:35:34.137: E/AndroidRuntime(968): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-26 12:35:34.137: E/AndroidRuntime(968):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.Activity.setContentView(Activity.java:1881)
11-26 12:35:34.137: E/AndroidRuntime(968):  at com.example.fragmenttest.MainActivity.onCreate(MainActivity.java:13)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.Activity.performCreate(Activity.java:5104)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-26 12:35:34.137: E/AndroidRuntime(968):  ... 11 more
11-26 12:35:34.137: E/AndroidRuntime(968): Caused by: java.lang.NullPointerException
11-26 12:35:34.137: E/AndroidRuntime(968):  at com.example.fragmenttest.FirstView.onCreateView(FirstView.java:19)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
11-26 12:35:34.137: E/AndroidRuntime(968):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
11-26 12:35:34.137: E/AndroidRuntime(968):  ... 21 more

5 个答案:

答案 0 :(得分:2)

至少,MainActivity应该延长FragmentActivity

之后你的片段:

此代码:

firstText = (TextView)getView().findViewById(R.id.viewOneText);
btn = (Button)getView().findViewById(R.id.viewOneBtn);

btn.setOnClickListener(new ButtonEvent());

应该在你夸大任何东西之后:

View view = inflater.inflate(R.layout.fragment_view1,container,false);

请返回片段文档以更深入地理解它们。现在看来你对如何使用碎片没有或多或少的清晰认识。您的问题将持续存在,直到您更好地了解碎片。祝你好运,欢迎回来,如果你有任何问题。

答案 1 :(得分:1)

您正在使用Android SDK 11中的Fragment和支持片段。

有两种解决方案:

  • 作为@Vladimir Ivanov建议,您可以使用FragmentActivity(这是支持片段的支持Activity)
  • 你可以使用android.app.Fragment(Android SDK 11+ Fragment与SDK 11 +的支持andorid.app.Activity配合得很好)

这确实有点令人困惑,因为有些类共享相同的名称。但您必须决定使用Android SDK 11+或支持库来支持SDK 7+中的设备。你不能混合这些类。

答案 2 :(得分:1)

您的MainActivity必须从支持库扩展FragmentActivity ..

在第一个片段的onCreateView()中,这是第一行

    View view = inflater.inflate(R.layout.fragment_view1,container,false);

并将getView()替换为view

答案 3 :(得分:1)

您正在尝试将SupportMapFragmen t转换为原生API Level 11 android.app.Fragment。那样不行。之一:

android:minSdkVersion设置为11或更高。

答案 4 :(得分:0)

问题很简单。你应该创建FragmentManager而FragmentTransaction是constance to manager fragment