1

我是 android 新手,并在网上寻找各种可用的教程来学习这些概念。

最近我遇到了 Fragments 并尝试实现它。

现在我正在尝试实现如何将片段动态添加到活动中。但是当我尝试在模拟器上启动我的应用程序时收到崩溃报告(模拟器不会崩溃)。

这是我的代码。

主要活动.java

public class MainActivity extends Activity {

    Fragment fragment;
    Button btn1, btn2, btn3;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        fragment = new Fragment1();
        btn1 = (Button) findViewById(R.id.btn1);
        
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_2, fragment);
        transaction.commit();
        
        btn1.setOnClickListener(btnClickListener);
    }

    @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;
    }   
    
    OnClickListener btnClickListener = new OnClickListener(){
        Fragment fragment;
        @SuppressLint("Recycle")
        @Override
        public void onClick(View v) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            if(v == btn1){
                fragment = new Fragment1();
            }
            transaction.replace(R.id.fragment_1,fragment);
            transaction.addToBackStack(null);
            transaction.commit();           
        }
        
    };
}

activity_main.xml

<LinearLayout 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=".MainActivity" >

<LinearLayout 
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:background="#333300">
    
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fragment 1"
        android:id="@+id/btn1"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/container"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    android:background="#000088"
    android:orientation="vertical" />

</LinearLayout>

Framgent1.java

@SuppressLint("NewApi")
public class Fragment1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.id.fragment_1, container, false);
    }
    
}

片段1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/fragment_1"
    android:background="#FFDDAA">
    

</LinearLayout>

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.apoos.dynamicfragment"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.apoos.dynamicfragment.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我有一个类似于fragment_1with name的片段fragment_2

Fragment_1 :默认 Fragment_2 :单击按钮时出现。

唯一的区别 b/w Fragment_1 和 Fragment_2 是它们有

不同的背景。

在启动模拟器时,模拟器会出现,但它说

不幸的是 MyApplication 已停止。

我也尝试使用支持库,但它有同样的问题。

谁能告诉我出了什么问题?或者任何人都可以指出我的android片段基础教程。

原木猫

10-11 11:20:29.395: E/FragmentManager(784): No view found for id 0x7f080004 (com.apoos.dynamicfragment:id/fragment_1) for fragment Fragment1{4171fa88 #0 id=0x7f080004}
10-11 11:20:29.405: E/FragmentManager(784): Activity state:
10-11 11:20:29.405: E/FragmentManager(784):   Local Activity 416f4e90 State:
10-11 11:20:29.405: E/FragmentManager(784):     mResumed=false mStopped=false mFinished=false
10-11 11:20:29.405: E/FragmentManager(784):     mLoadersStarted=false
10-11 11:20:29.405: E/FragmentManager(784):     mChangingConfigurations=false
10-11 11:20:29.405: E/FragmentManager(784):     mCurrentConfig={1.0 310mcc260mnc en_US ldltr sw320dp w320dp h508dp 240dpi nrml long port finger qwerty/v/v -nav/h s.6}
10-11 11:20:29.405: E/FragmentManager(784):   Active Fragments in 416f4f90:
10-11 11:20:29.405: E/FragmentManager(784):     #0: Fragment1{4171fa88 #0 id=0x7f080004}
10-11 11:20:29.405: E/FragmentManager(784):       mFragmentId=#7f080004 mContainerId=#7f080004 mTag=null
10-11 11:20:29.405: E/FragmentManager(784):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
10-11 11:20:29.405: E/FragmentManager(784):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-11 11:20:29.405: E/FragmentManager(784):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
10-11 11:20:29.416: E/FragmentManager(784):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
10-11 11:20:29.416: E/FragmentManager(784):       mFragmentManager=FragmentManager{416f4f90 in MainActivity{416f4e90}}
10-11 11:20:29.416: E/FragmentManager(784):       mActivity=com.apoos.dynamicfragment.MainActivity@416f4e90
10-11 11:20:29.416: E/FragmentManager(784):   Added Fragments:
10-11 11:20:29.416: E/FragmentManager(784):     #0: Fragment1{4171fa88 #0 id=0x7f080004}
10-11 11:20:29.416: E/FragmentManager(784):   FragmentManager misc state:
10-11 11:20:29.416: E/FragmentManager(784):     mActivity=com.apoos.dynamicfragment.MainActivity@416f4e90
10-11 11:20:29.416: E/FragmentManager(784):     mContainer=android.app.Activity$1@416f5008
10-11 11:20:29.416: E/FragmentManager(784):     mCurState=2 mStateSaved=false mDestroyed=false
10-11 11:20:29.416: E/FragmentManager(784):   View Hierarchy:
10-11 11:20:29.416: E/FragmentManager(784):     com.android.internal.policy.impl.PhoneWindow$DecorView{416f6168 V.E..... R.....ID 0,0-0,0}
10-11 11:20:29.425: E/FragmentManager(784):       com.android.internal.widget.ActionBarOverlayLayout{416f71f8 V.E..... ......I. 0,0-0,0 #102033d android:id/action_bar_overlay_layout}
10-11 11:20:29.425: E/FragmentManager(784):         android.widget.FrameLayout{416f7f90 V.E..... ......I. 0,0-0,0 #1020002 android:id/content}
10-11 11:20:29.425: E/FragmentManager(784):           android.widget.LinearLayout{41712180 V.E..... ......I. 0,0-0,0}
10-11 11:20:29.425: E/FragmentManager(784):             android.widget.LinearLayout{41712528 V.E..... ......ID 0,0-0,0}
10-11 11:20:29.425: E/FragmentManager(784):               android.widget.Button{41712b50 VFED..C. ......I. 0,0-0,0 #7f080000 app:id/btn1}
10-11 11:20:29.425: E/FragmentManager(784):               android.widget.Button{4171dbc8 VFED..C. ......I. 0,0-0,0 #7f080001 app:id/btn2}
10-11 11:20:29.425: E/FragmentManager(784):               android.widget.Button{4171e660 VFED..C. ......I. 0,0-0,0 #7f080002 app:id/btn3}
10-11 11:20:29.435: E/FragmentManager(784):             android.widget.LinearLayout{4171f0f8 V.E..... ......ID 0,0-0,0 #7f080003 app:id/container}
10-11 11:20:29.435: E/FragmentManager(784):         android.widget.LinearLayout{416f85b8 V.E..... ......I. 0,0-0,0 #102033e android:id/top_action_bar}
10-11 11:20:29.435: E/FragmentManager(784):           com.android.internal.widget.ActionBarContainer{416f8a60 V.ED.... ......ID 0,0-0,0 #102033f android:id/action_bar_container}
10-11 11:20:29.435: E/FragmentManager(784):             com.android.internal.widget.ActionBarView{416fbea0 V.E..... ......ID 0,0-0,0 #1020340 android:id/action_bar}
10-11 11:20:29.435: E/FragmentManager(784):               android.widget.LinearLayout{41702750 V.....C. ......I. 0,0-0,0}
10-11 11:20:29.435: E/FragmentManager(784):                 com.android.internal.widget.ActionBarView$HomeView{41706938 V.E..... ......I. 0,0-0,0}
10-11 11:20:29.435: E/FragmentManager(784):                   android.widget.ImageView{41706ed0 G.ED.... ......I. 0,0-0,0 #1020252 android:id/up}
10-11 11:20:29.435: E/FragmentManager(784):                   android.widget.ImageView{41707e48 V.ED.... ......I. 0,0-0,0 #102002c android:id/home}
10-11 11:20:29.435: E/FragmentManager(784):                 android.widget.LinearLayout{41709860 G.E..... ......I. 0,0-0,0}
10-11 11:20:29.435: E/FragmentManager(784):                   android.widget.ImageView{41709c50 G.ED.... ......I. 0,0-0,0 #1020252 android:id/up}
10-11 11:20:29.435: E/FragmentManager(784):                   android.widget.LinearLayout{41709fe0 V.E..... ......I. 0,0-0,0}
10-11 11:20:29.445: E/FragmentManager(784):                     android.widget.TextView{4170a558 V.ED.... ......ID 0,0-0,0 #102025a android:id/action_bar_title}
10-11 11:20:29.445: E/FragmentManager(784):                     android.widget.TextView{4170b2e0 G.ED.... ......I. 0,0-0,0 #102025b android:id/action_bar_subtitle}
10-11 11:20:29.445: E/FragmentManager(784):             com.android.internal.widget.ActionBarContextView{4170b9e0 G.E..... ......ID 0,0-0,0 #1020341 android:id/action_context_bar}
10-11 11:20:29.445: E/FragmentManager(784):           android.widget.ImageView{4170ece0 V.ED.... ......I. 0,0-0,0}
10-11 11:20:29.445: E/FragmentManager(784):         com.android.internal.widget.ActionBarContainer{4170fbd8 G.ED.... ......ID 0,0-0,0 #1020342 android:id/split_action_bar}
10-11 11:20:29.445: D/AndroidRuntime(784): Shutting down VM
10-11 11:20:29.445: W/dalvikvm(784): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-11 11:20:29.455: E/AndroidRuntime(784): FATAL EXCEPTION: main
10-11 11:20:29.455: E/AndroidRuntime(784): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apoos.dynamicfragment/com.apoos.dynamicfragment.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080004 (com.apoos.dynamicfragment:id/fragment_1) for fragment Fragment1{4171fa88 #0 id=0x7f080004}
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.os.Looper.loop(Looper.java:137)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-11 11:20:29.455: E/AndroidRuntime(784):  at java.lang.reflect.Method.invokeNative(Native Method)
10-11 11:20:29.455: E/AndroidRuntime(784):  at java.lang.reflect.Method.invoke(Method.java:525)
10-11 11:20:29.455: E/AndroidRuntime(784):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-11 11:20:29.455: E/AndroidRuntime(784):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-11 11:20:29.455: E/AndroidRuntime(784):  at dalvik.system.NativeStart.main(Native Method)
10-11 11:20:29.455: E/AndroidRuntime(784): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080004 (com.apoos.dynamicfragment:id/fragment_1) for fragment Fragment1{4171fa88 #0 id=0x7f080004}
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.BackStackRecord.run(BackStackRecord.java:682)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.Activity.performStart(Activity.java:5142)
10-11 11:20:29.455: E/AndroidRuntime(784):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-11 11:20:29.455: E/AndroidRuntime(784):  ... 11 more
10-11 11:20:33.955: I/Process(784): Sending signal. PID: 784 SIG: 9
10-11 11:22:14.755: E/FragmentManager(839): No view found for id 0x7f080004 (com.apoos.dynamicfragment:id/fragment_1) for fragment Fragment1{41720898 #0 id=0x7f080004}
10-11 11:22:14.755: E/FragmentManager(839): Activity state:
10-11 11:22:14.755: E/FragmentManager(839):   Local Activity 416f5ca0 State:
10-11 11:22:14.765: E/FragmentManager(839):     mResumed=false mStopped=false mFinished=false
10-11 11:22:14.765: E/FragmentManager(839):     mLoadersStarted=false
10-11 11:22:14.765: E/FragmentManager(839):     mChangingConfigurations=false
10-11 11:22:14.765: E/FragmentManager(839):     mCurrentConfig={1.0 310mcc260mnc en_US ldltr sw320dp w320dp h508dp 240dpi nrml long port finger qwerty/v/v -nav/h s.6}
10-11 11:22:14.765: E/FragmentManager(839):   Active Fragments in 416f5da0:
10-11 11:22:14.765: E/FragmentManager(839):     #0: Fragment1{41720898 #0 id=0x7f080004}
10-11 11:22:14.765: E/FragmentManager(839):       mFragmentId=#7f080004 mContainerId=#7f080004 mTag=null
10-11 11:22:14.765: E/FragmentManager(839):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
10-11 11:22:14.765: E/FragmentManager(839):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-11 11:22:14.765: E/FragmentManager(839):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
10-11 11:22:14.765: E/FragmentManager(839):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
10-11 11:22:14.796: E/FragmentManager(839):       mFragmentManager=FragmentManager{416f5da0 in MainActivity{416f5ca0}}
10-11 11:22:14.796: E/FragmentManager(839):       mActivity=com.apoos.dynamicfragment.MainActivity@416f5ca0
10-11 11:22:14.806: E/FragmentManager(839):   Added Fragments:
10-11 11:22:14.826: E/FragmentManager(839):     #0: Fragment1{41720898 #0 id=0x7f080004}
10-11 11:22:14.826: E/FragmentManager(839):   FragmentManager misc state:
10-11 11:22:14.826: E/FragmentManager(839):     mActivity=com.apoos.dynamicfragment.MainActivity@416f5ca0
10-11 11:22:14.826: E/FragmentManager(839):     mContainer=android.app.Activity$1@416f5e18
10-11 11:22:14.826: E/FragmentManager(839):     mCurState=2 mStateSaved=false mDestroyed=false
10-11 11:22:14.826: E/FragmentManager(839):   View Hierarchy:
10-11 11:22:14.826: E/FragmentManager(839):     com.android.internal.policy.impl.PhoneWindow$DecorView{416f6f78 V.E..... R.....ID 0,0-0,0}
10-11 11:22:14.826: E/FragmentManager(839):       com.android.internal.widget.ActionBarOverlayLayout{416f8008 V.E..... ......I. 0,0-0,0 #102033d android:id/action_bar_overlay_layout}
10-11 11:22:14.826: E/FragmentManager(839):         android.widget.FrameLayout{416f8da0 V.E..... ......I. 0,0-0,0 #1020002 android:id/content}
10-11 11:22:14.836: E/FragmentManager(839):           android.widget.LinearLayout{41712f90 V.E..... ......I. 0,0-0,0}
10-11 11:22:14.836: E/FragmentManager(839):             android.widget.LinearLayout{41713338 V.E..... ......ID 0,0-0,0}
10-11 11:22:14.836: E/FragmentManager(839):               android.widget.Button{41713960 VFED..C. ......I. 0,0-0,0 #7f080000 app:id/btn1}
10-11 11:22:14.836: E/FragmentManager(839):               android.widget.Button{4171e9d8 VFED..C. ......I. 0,0-0,0 #7f080001 app:id/btn2}
10-11 11:22:14.836: E/FragmentManager(839):               android.widget.Button{4171f470 VFED..C. ......I. 0,0-0,0 #7f080002 app:id/btn3}
10-11 11:22:14.836: E/FragmentManager(839):             android.widget.LinearLayout{4171ff08 V.E..... ......ID 0,0-0,0 #7f080003 app:id/container}
10-11 11:22:14.836: E/FragmentManager(839):         android.widget.LinearLayout{416f93c8 V.E..... ......I. 0,0-0,0 #102033e android:id/top_action_bar}
10-11 11:22:14.836: E/FragmentManager(839):           com.android.internal.widget.ActionBarContainer{416f9870 V.ED.... ......ID 0,0-0,0 #102033f android:id/action_bar_container}
10-11 11:22:14.836: E/FragmentManager(839):             com.android.internal.widget.ActionBarView{416fccb0 V.E..... ......ID 0,0-0,0 #1020340 android:id/action_bar}
10-11 11:22:14.846: E/FragmentManager(839):               android.widget.LinearLayout{41703560 V.....C. ......I. 0,0-0,0}
10-11 11:22:14.846: E/FragmentManager(839):                 com.android.internal.widget.ActionBarView$HomeView{41707748 V.E..... ......I. 0,0-0,0}
10-11 11:22:14.846: E/FragmentManager(839):                   android.widget.ImageView{41707ce0 G.ED.... ......I. 0,0-0,0 #1020252 android:id/up}
10-11 11:22:14.846: E/FragmentManager(839):                   android.widget.ImageView{41708c58 V.ED.... ......I. 0,0-0,0 #102002c android:id/home}
10-11 11:22:14.846: E/FragmentManager(839):                 android.widget.LinearLayout{4170a670 G.E..... ......I. 0,0-0,0}
10-11 11:22:14.846: E/FragmentManager(839):                   android.widget.ImageView{4170aa60 G.ED.... ......I. 0,0-0,0 #1020252 android:id/up}
10-11 11:22:14.846: E/FragmentManager(839):                   android.widget.LinearLayout{4170adf0 V.E..... ......I. 0,0-0,0}
10-11 11:22:14.846: E/FragmentManager(839):                     android.widget.TextView{4170b368 V.ED.... ......ID 0,0-0,0 #102025a android:id/action_bar_title}
10-11 11:22:14.846: E/FragmentManager(839):                     android.widget.TextView{4170c0f0 G.ED.... ......I. 0,0-0,0 #102025b android:id/action_bar_subtitle}
10-11 11:22:14.846: E/FragmentManager(839):             com.android.internal.widget.ActionBarContextView{4170c7f0 G.E..... ......ID 0,0-0,0 #1020341 android:id/action_context_bar}
10-11 11:22:14.846: E/FragmentManager(839):           android.widget.ImageView{4170faf0 V.ED.... ......I. 0,0-0,0}
10-11 11:22:14.846: E/FragmentManager(839):         com.android.internal.widget.ActionBarContainer{417109e8 G.ED.... ......ID 0,0-0,0 #1020342 android:id/split_action_bar}
10-11 11:22:14.856: D/AndroidRuntime(839): Shutting down VM
10-11 11:22:14.856: W/dalvikvm(839): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-11 11:22:14.866: E/AndroidRuntime(839): FATAL EXCEPTION: main
10-11 11:22:14.866: E/AndroidRuntime(839): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apoos.dynamicfragment/com.apoos.dynamicfragment.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080004 (com.apoos.dynamicfragment:id/fragment_1) for fragment Fragment1{41720898 #0 id=0x7f080004}
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.os.Looper.loop(Looper.java:137)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-11 11:22:14.866: E/AndroidRuntime(839):  at java.lang.reflect.Method.invokeNative(Native Method)
10-11 11:22:14.866: E/AndroidRuntime(839):  at java.lang.reflect.Method.invoke(Method.java:525)
10-11 11:22:14.866: E/AndroidRuntime(839):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-11 11:22:14.866: E/AndroidRuntime(839):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-11 11:22:14.866: E/AndroidRuntime(839):  at dalvik.system.NativeStart.main(Native Method)
10-11 11:22:14.866: E/AndroidRuntime(839): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080004 (com.apoos.dynamicfragment:id/fragment_1) for fragment Fragment1{41720898 #0 id=0x7f080004}
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.BackStackRecord.run(BackStackRecord.java:682)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.Activity.performStart(Activity.java:5142)
10-11 11:22:14.866: E/AndroidRuntime(839):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-11 11:22:14.866: E/AndroidRuntime(839):  ... 11 more
10-11 11:22:22.295: I/Process(839): Sending signal. PID: 839 SIG: 9
4

3 回答 3

1

你应该膨胀一个布局。

将以下内容视为 fragment_1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/fragment_1"
    android:background="#FFDDAA">


</LinearLayout>

片段1的变化

  return inflater.inflate(R.id.fragment_1, container, false);

  return inflater.inflate(R.layout.fragment_1, container, false);

在你的MainActivity

改变

  transaction.add(R.id.fragment_2, fragment);

  transaction.add(R.id.container, fragment);

bcoz 您正在将片段添加到容器中,这LinearLayout在您的情况下是

<LinearLayout
    android:id="@+id/container" // id is container not fragment_1
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    android:background="#000088"
    android:orientation="vertical" />

</LinearLayout>

如果你有 Fragment2.java,同样在按钮单击

  fragment = new Fragment2(); // Framgent2
  transaction.replace(R.id.container,fragment); 
  // replace Fragment1 by Fragment 2 in the container
  transaction.addToBackStack(null);
于 2013-10-11T05:59:43.903 回答
0

首先,您必须在使用FragmentActivity时使用Fragment

然后,如果它再次强制关闭,则发布 logcat 错误代码

于 2013-10-11T05:43:49.800 回答
0

在您的MainActivity更改中,如下所示:

而不是 idtransaction.add(R.id.fragment_2, fragment);将其更改为transaction.add(R.id.container, fragment);

按钮单击也是如此。

public class MainActivity extends Activity {

    Fragment fragment;
    Button btn1, btn2, btn3;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragment = new Fragment1();
        btn1 = (Button) findViewById(R.id.btn1);

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.container, fragment);
        transaction.commit();

    }
于 2013-10-11T06:12:57.267 回答