首页 \ 问答 \ Logcat错误:无法在android片段中加载视图(Logcat error: unable to load view in android fragments)

Logcat错误:无法在android片段中加载视图(Logcat error: unable to load view in android fragments)

我试图从活动切换到片段。 我确实认为我的代码是正确的。 但我仍然在logcat中收到错误,它无法加载视图。 请告诉我我错在哪里。

我的LOGCAT:

10-01 14:53:58.314: E/FragmentManager(1048): No view found for id 0x7f090001 (com.example.test:id/content_frame) for fragment WordFragment{418ae390 #0 id=0x7f090001}
10-01 14:53:58.314: E/FragmentManager(1048): Activity state:
10-01 14:53:58.324: E/FragmentManager(1048):   Local Activity 4184bfe0 State:
10-01 14:53:58.334: E/FragmentManager(1048):     mResumed=true mStopped=false mFinished=false
10-01 14:53:58.344: E/FragmentManager(1048):     mLoadersStarted=true
10-01 14:53:58.344: E/FragmentManager(1048):     mChangingConfigurations=false
10-01 14:53:58.355: E/FragmentManager(1048):     mCurrentConfig={1.0 310mcc260mnc en_US ldltr sw320dp w320dp h508dp 240dpi nrml long port finger qwerty/v/v -nav/h s.6}
10-01 14:53:58.355: E/FragmentManager(1048):   Active Fragments in 4184c110:
10-01 14:53:58.364: E/FragmentManager(1048):     #0: WordFragment{418ae390 #0 id=0x7f090001}
10-01 14:53:58.364: E/FragmentManager(1048):       mFragmentId=#7f090001 mContainerId=#7f090001 mTag=null
10-01 14:53:58.374: E/FragmentManager(1048):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=1
10-01 14:53:58.374: E/FragmentManager(1048):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-01 14:53:58.384: E/FragmentManager(1048):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
10-01 14:53:58.394: E/FragmentManager(1048):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
10-01 14:53:58.405: E/FragmentManager(1048):       mFragmentManager=FragmentManager{4184c110 in Home{4184bfe0}}
10-01 14:53:58.414: E/FragmentManager(1048):       mActivity=com.example.test.Home@4184bfe0
10-01 14:53:58.414: E/FragmentManager(1048):   Added Fragments:
10-01 14:53:58.425: E/FragmentManager(1048):     #0: WordFragment{418ae390 #0 id=0x7f090001}
10-01 14:53:58.425: E/FragmentManager(1048):   Back Stack Indices:
10-01 14:53:58.425: E/FragmentManager(1048):     #0: BackStackEntry{418ae440 #0}
10-01 14:53:58.434: E/FragmentManager(1048):   FragmentManager misc state:
10-01 14:53:58.434: E/FragmentManager(1048):     mActivity=com.example.test.Home@4184bfe0
10-01 14:53:58.444: E/FragmentManager(1048):     mContainer=android.app.Activity$1@4184c188
10-01 14:53:58.455: E/FragmentManager(1048):     mCurState=5 mStateSaved=false mDestroyed=false
10-01 14:53:58.455: E/FragmentManager(1048):   View Hierarchy:
10-01 14:53:58.455: E/FragmentManager(1048):     com.android.internal.policy.impl.PhoneWindow$DecorView{4184cca8 V.E..... R.....ID 0,0-480,800}
10-01 14:53:58.474: E/FragmentManager(1048):       com.android.internal.widget.ActionBarOverlayLayout{4184d3c8 V.E..... ......ID 0,0-480,800 #102033d android:id/action_bar_overlay_layout}
10-01 14:53:58.474: E/FragmentManager(1048):         android.widget.FrameLayout{4184e038 V.E..... ......ID 0,110-480,800 #1020002 android:id/content}
10-01 14:53:58.484: E/FragmentManager(1048):           android.widget.RelativeLayout{41856d18 V.E..... ......ID 0,0-480,690}
10-01 14:53:58.495: E/FragmentManager(1048):             android.widget.TableLayout{41857488 V.E..... ......I. 24,333-456,666 #7f09000d app:id/tableLayout1}
10-01 14:53:58.495: E/FragmentManager(1048):               android.widget.TableRow{41857c78 V.E..... ......I. 0,0-432,87 #7f09000e app:id/tableRow1}
10-01 14:53:58.504: E/FragmentManager(1048):                 android.widget.Button{418581b8 VFED..C. ......I. 0,0-208,72 #7f090018 app:id/button1}
10-01 14:53:58.514: E/FragmentManager(1048):                 android.widget.Button{41864768 VFED..C. ......I. 223,0-432,72 #7f090019 app:id/button2}
10-01 14:53:58.514: E/FragmentManager(1048):               android.widget.TableRow{41864d30 V.E..... ......I. 0,87-432,174 #7f090010 app:id/tableRow2}
10-01 14:53:58.524: E/FragmentManager(1048):                 android.widget.Button{41865108 VFED..C. ......I. 0,0-208,72 #7f09001a app:id/button3}
10-01 14:53:58.535: E/FragmentManager(1048):                 android.widget.Button{418656c8 VFED..C. ......I. 223,0-432,72 #7f09001b app:id/button4}
10-01 14:53:58.535: E/FragmentManager(1048):               android.widget.TableRow{41865c88 V.E..... ......I. 0,174-432,246 #7f090012 app:id/tableRow3}
10-01 14:53:58.544: E/FragmentManager(1048):                 android.widget.Button{41866060 VFED..C. ......I. 0,0-208,72 #7f09001c app:id/button5}
10-01 14:53:58.544: E/FragmentManager(1048):                 android.widget.Button{41866630 VFED..C. ......I. 223,0-432,72 #7f09001d app:id/button6}
10-01 14:53:58.544: E/FragmentManager(1048):               android.widget.TableRow{41866be8 V.E..... ......I. 0,261-432,333 #7f090014 app:id/tableRow4}
10-01 14:53:58.554: E/FragmentManager(1048):                 android.widget.Button{41866fc0 VFED..C. ......I. 0,0-208,72 #7f09001e app:id/button7}
10-01 14:53:58.554: E/FragmentManager(1048):                 android.widget.Button{41867580 VFED..C. ......I. 223,0-432,72 #7f09001f app:id/button8}
10-01 14:53:58.564: E/FragmentManager(1048):             android.widget.ImageView{41867b88 V.ED.... ......I. 24,24-456,99 #7f090003 app:id/imageView1}
10-01 14:53:58.564: E/FragmentManager(1048):             android.widget.ScrollView{4173e9d0 VFED.V.. ......ID 24,99-456,306 #7f090020 app:id/ScrollView01}
10-01 14:53:58.574: E/FragmentManager(1048):               android.widget.RelativeLayout{41773b10 V.E..... ......ID 0,0-432,174 #7f090021 app:id/RelativeLayout01}
10-01 14:53:58.574: E/FragmentManager(1048):                 android.widget.LinearLayout{41776658 V.E..... ......ID 0,0-432,174 #7f090022 app:id/LinearLayout01}
10-01 14:53:58.574: E/FragmentManager(1048):                   android.widget.TextView{41747670 V.ED.... ......I. 172,0-259,29 #7f090004 app:id/textView1}
10-01 14:53:58.584: E/FragmentManager(1048):                   android.widget.TextView{41758a38 V.ED.... ......I. 172,29-259,58 #7f090005 app:id/textView2}
10-01 14:53:58.584: E/FragmentManager(1048):                   android.widget.TextView{417538a0 V.ED.... ......I. 172,58-259,87 #7f090008 app:id/textView3}
10-01 14:53:58.594: E/FragmentManager(1048):                   android.widget.TextView{41753b40 V.ED.... ......I. 172,87-259,116 #7f090007 app:id/textView4}
10-01 14:53:58.604: E/FragmentManager(1048):                   android.widget.TextView{41850368 V.ED.... ......I. 172,116-259,145 #7f090006 app:id/textView5}
10-01 14:53:58.614: E/FragmentManager(1048):                   android.widget.TextView{41850608 V.ED.... ......I. 172,145-259,174 #7f090009 app:id/textView6}
10-01 14:53:58.614: E/FragmentManager(1048):         android.widget.LinearLayout{4184e490 V.E..... ......I. 0,38-480,110 #102033e android:id/top_action_bar}
10-01 14:53:58.624: E/FragmentManager(1048):           com.android.internal.widget.ActionBarContainer{4184e8c8 V.ED.... ......I. 0,0-480,72 #102033f android:id/action_bar_container}
10-01 14:53:58.624: E/FragmentManager(1048):             com.android.internal.widget.ActionBarView{4184eeb0 V.E..... ......I. 0,0-480,72 #1020340 android:id/action_bar}
10-01 14:53:58.634: E/FragmentManager(1048):               android.widget.LinearLayout{41850090 V.....C. ......I. 13,0-131,72}
10-01 14:53:58.634: E/FragmentManager(1048):                 com.android.internal.widget.ActionBarView$HomeView{41851798 V.E..... ......I. 0,0-60,72}
10-01 14:53:58.644: E/FragmentManager(1048):                   android.widget.ImageView{41851c08 G.ED.... ......I. 0,0-0,0 #1020252 android:id/up}
10-01 14:53:58.655: E/FragmentManager(1048):                   android.widget.ImageView{41851f90 V.ED.... ......I. 6,12-54,60 #102002c android:id/home}
10-01 14:53:58.655: E/FragmentManager(1048):                 android.widget.LinearLayout{418538d8 V.E..... ......I. 60,0-118,72}
10-01 14:53:58.674: E/FragmentManager(1048):                   android.widget.ImageView{41853cc8 G.ED.... ......I. 0,0-0,0 #1020252 android:id/up}
10-01 14:53:58.674: E/FragmentManager(1048):                   android.widget.LinearLayout{41854058 V.E..... ......I. 0,17-46,54}
10-01 14:53:58.684: E/FragmentManager(1048):                     android.widget.TextView{41854450 V.ED.... ......I. 0,0-46,37 #102025a android:id/action_bar_title}
10-01 14:53:58.694: E/FragmentManager(1048):                     android.widget.TextView{41855178 G.ED.... ......I. 0,0-0,0 #102025b android:id/action_bar_subtitle}
10-01 14:53:58.705: E/FragmentManager(1048):               com.android.internal.view.menu.ActionMenuView{418a5e30 V.ED.... ......I. 480,0-480,72}
10-01 14:53:58.714: E/FragmentManager(1048):             com.android.internal.widget.ActionBarContextView{418557b8 G.E..... ......ID 0,0-0,0 #1020341 android:id/action_context_bar}
10-01 14:53:58.714: E/FragmentManager(1048):           android.widget.ImageView{41855d28 V.ED.... ......I. 480,0-480,24}
10-01 14:53:58.734: E/FragmentManager(1048):         com.android.internal.widget.ActionBarContainer{418560a0 G.ED.... ......ID 0,0-0,0 #1020342 android:id/split_action_bar}
10-01 14:53:58.734: D/AndroidRuntime(1048): Shutting down VM
10-01 14:53:58.734: W/dalvikvm(1048): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-01 14:53:58.804: E/AndroidRuntime(1048): FATAL EXCEPTION: main
10-01 14:53:58.804: E/AndroidRuntime(1048): java.lang.IllegalArgumentException: No view found for id 0x7f090001 (com.example.test:id/content_frame) for fragment WordFragment{418ae390 #0 id=0x7f090001}
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.os.Handler.handleCallback(Handler.java:730)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.os.Looper.loop(Looper.java:137)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at java.lang.reflect.Method.invoke(Method.java:525)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at dalvik.system.NativeStart.main(Native Method)
10-01 14:54:01.205: I/Process(1048): Sending signal. PID: 1048 SIG: 9

我的活动文件:

package com.example.test;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Home extends Activity implements OnClickListener {
Button btn1;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    FrameLayout frame = (FrameLayout) findViewById(R.id.content_frame);
    btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

        Fragment newFragment = new WordFragment();
        FragmentTransaction transaction = getFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.content_frame, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();

        break;

 }
 }

我的WordFragment类:

package com.example.test;

import android.app.Fragment;
import android.app.FragmentTransaction;   
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class WordFragment extends Fragment {

public WordFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

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

    return view;
}
}

I am trying to switch from activity to fragment. I do think so that my code is correct. but i still am getting the error in logcat that it is unable to load the view. Please tell me where i am wrong.

My LOGCAT:

10-01 14:53:58.314: E/FragmentManager(1048): No view found for id 0x7f090001 (com.example.test:id/content_frame) for fragment WordFragment{418ae390 #0 id=0x7f090001}
10-01 14:53:58.314: E/FragmentManager(1048): Activity state:
10-01 14:53:58.324: E/FragmentManager(1048):   Local Activity 4184bfe0 State:
10-01 14:53:58.334: E/FragmentManager(1048):     mResumed=true mStopped=false mFinished=false
10-01 14:53:58.344: E/FragmentManager(1048):     mLoadersStarted=true
10-01 14:53:58.344: E/FragmentManager(1048):     mChangingConfigurations=false
10-01 14:53:58.355: E/FragmentManager(1048):     mCurrentConfig={1.0 310mcc260mnc en_US ldltr sw320dp w320dp h508dp 240dpi nrml long port finger qwerty/v/v -nav/h s.6}
10-01 14:53:58.355: E/FragmentManager(1048):   Active Fragments in 4184c110:
10-01 14:53:58.364: E/FragmentManager(1048):     #0: WordFragment{418ae390 #0 id=0x7f090001}
10-01 14:53:58.364: E/FragmentManager(1048):       mFragmentId=#7f090001 mContainerId=#7f090001 mTag=null
10-01 14:53:58.374: E/FragmentManager(1048):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=1
10-01 14:53:58.374: E/FragmentManager(1048):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
10-01 14:53:58.384: E/FragmentManager(1048):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
10-01 14:53:58.394: E/FragmentManager(1048):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
10-01 14:53:58.405: E/FragmentManager(1048):       mFragmentManager=FragmentManager{4184c110 in Home{4184bfe0}}
10-01 14:53:58.414: E/FragmentManager(1048):       mActivity=com.example.test.Home@4184bfe0
10-01 14:53:58.414: E/FragmentManager(1048):   Added Fragments:
10-01 14:53:58.425: E/FragmentManager(1048):     #0: WordFragment{418ae390 #0 id=0x7f090001}
10-01 14:53:58.425: E/FragmentManager(1048):   Back Stack Indices:
10-01 14:53:58.425: E/FragmentManager(1048):     #0: BackStackEntry{418ae440 #0}
10-01 14:53:58.434: E/FragmentManager(1048):   FragmentManager misc state:
10-01 14:53:58.434: E/FragmentManager(1048):     mActivity=com.example.test.Home@4184bfe0
10-01 14:53:58.444: E/FragmentManager(1048):     mContainer=android.app.Activity$1@4184c188
10-01 14:53:58.455: E/FragmentManager(1048):     mCurState=5 mStateSaved=false mDestroyed=false
10-01 14:53:58.455: E/FragmentManager(1048):   View Hierarchy:
10-01 14:53:58.455: E/FragmentManager(1048):     com.android.internal.policy.impl.PhoneWindow$DecorView{4184cca8 V.E..... R.....ID 0,0-480,800}
10-01 14:53:58.474: E/FragmentManager(1048):       com.android.internal.widget.ActionBarOverlayLayout{4184d3c8 V.E..... ......ID 0,0-480,800 #102033d android:id/action_bar_overlay_layout}
10-01 14:53:58.474: E/FragmentManager(1048):         android.widget.FrameLayout{4184e038 V.E..... ......ID 0,110-480,800 #1020002 android:id/content}
10-01 14:53:58.484: E/FragmentManager(1048):           android.widget.RelativeLayout{41856d18 V.E..... ......ID 0,0-480,690}
10-01 14:53:58.495: E/FragmentManager(1048):             android.widget.TableLayout{41857488 V.E..... ......I. 24,333-456,666 #7f09000d app:id/tableLayout1}
10-01 14:53:58.495: E/FragmentManager(1048):               android.widget.TableRow{41857c78 V.E..... ......I. 0,0-432,87 #7f09000e app:id/tableRow1}
10-01 14:53:58.504: E/FragmentManager(1048):                 android.widget.Button{418581b8 VFED..C. ......I. 0,0-208,72 #7f090018 app:id/button1}
10-01 14:53:58.514: E/FragmentManager(1048):                 android.widget.Button{41864768 VFED..C. ......I. 223,0-432,72 #7f090019 app:id/button2}
10-01 14:53:58.514: E/FragmentManager(1048):               android.widget.TableRow{41864d30 V.E..... ......I. 0,87-432,174 #7f090010 app:id/tableRow2}
10-01 14:53:58.524: E/FragmentManager(1048):                 android.widget.Button{41865108 VFED..C. ......I. 0,0-208,72 #7f09001a app:id/button3}
10-01 14:53:58.535: E/FragmentManager(1048):                 android.widget.Button{418656c8 VFED..C. ......I. 223,0-432,72 #7f09001b app:id/button4}
10-01 14:53:58.535: E/FragmentManager(1048):               android.widget.TableRow{41865c88 V.E..... ......I. 0,174-432,246 #7f090012 app:id/tableRow3}
10-01 14:53:58.544: E/FragmentManager(1048):                 android.widget.Button{41866060 VFED..C. ......I. 0,0-208,72 #7f09001c app:id/button5}
10-01 14:53:58.544: E/FragmentManager(1048):                 android.widget.Button{41866630 VFED..C. ......I. 223,0-432,72 #7f09001d app:id/button6}
10-01 14:53:58.544: E/FragmentManager(1048):               android.widget.TableRow{41866be8 V.E..... ......I. 0,261-432,333 #7f090014 app:id/tableRow4}
10-01 14:53:58.554: E/FragmentManager(1048):                 android.widget.Button{41866fc0 VFED..C. ......I. 0,0-208,72 #7f09001e app:id/button7}
10-01 14:53:58.554: E/FragmentManager(1048):                 android.widget.Button{41867580 VFED..C. ......I. 223,0-432,72 #7f09001f app:id/button8}
10-01 14:53:58.564: E/FragmentManager(1048):             android.widget.ImageView{41867b88 V.ED.... ......I. 24,24-456,99 #7f090003 app:id/imageView1}
10-01 14:53:58.564: E/FragmentManager(1048):             android.widget.ScrollView{4173e9d0 VFED.V.. ......ID 24,99-456,306 #7f090020 app:id/ScrollView01}
10-01 14:53:58.574: E/FragmentManager(1048):               android.widget.RelativeLayout{41773b10 V.E..... ......ID 0,0-432,174 #7f090021 app:id/RelativeLayout01}
10-01 14:53:58.574: E/FragmentManager(1048):                 android.widget.LinearLayout{41776658 V.E..... ......ID 0,0-432,174 #7f090022 app:id/LinearLayout01}
10-01 14:53:58.574: E/FragmentManager(1048):                   android.widget.TextView{41747670 V.ED.... ......I. 172,0-259,29 #7f090004 app:id/textView1}
10-01 14:53:58.584: E/FragmentManager(1048):                   android.widget.TextView{41758a38 V.ED.... ......I. 172,29-259,58 #7f090005 app:id/textView2}
10-01 14:53:58.584: E/FragmentManager(1048):                   android.widget.TextView{417538a0 V.ED.... ......I. 172,58-259,87 #7f090008 app:id/textView3}
10-01 14:53:58.594: E/FragmentManager(1048):                   android.widget.TextView{41753b40 V.ED.... ......I. 172,87-259,116 #7f090007 app:id/textView4}
10-01 14:53:58.604: E/FragmentManager(1048):                   android.widget.TextView{41850368 V.ED.... ......I. 172,116-259,145 #7f090006 app:id/textView5}
10-01 14:53:58.614: E/FragmentManager(1048):                   android.widget.TextView{41850608 V.ED.... ......I. 172,145-259,174 #7f090009 app:id/textView6}
10-01 14:53:58.614: E/FragmentManager(1048):         android.widget.LinearLayout{4184e490 V.E..... ......I. 0,38-480,110 #102033e android:id/top_action_bar}
10-01 14:53:58.624: E/FragmentManager(1048):           com.android.internal.widget.ActionBarContainer{4184e8c8 V.ED.... ......I. 0,0-480,72 #102033f android:id/action_bar_container}
10-01 14:53:58.624: E/FragmentManager(1048):             com.android.internal.widget.ActionBarView{4184eeb0 V.E..... ......I. 0,0-480,72 #1020340 android:id/action_bar}
10-01 14:53:58.634: E/FragmentManager(1048):               android.widget.LinearLayout{41850090 V.....C. ......I. 13,0-131,72}
10-01 14:53:58.634: E/FragmentManager(1048):                 com.android.internal.widget.ActionBarView$HomeView{41851798 V.E..... ......I. 0,0-60,72}
10-01 14:53:58.644: E/FragmentManager(1048):                   android.widget.ImageView{41851c08 G.ED.... ......I. 0,0-0,0 #1020252 android:id/up}
10-01 14:53:58.655: E/FragmentManager(1048):                   android.widget.ImageView{41851f90 V.ED.... ......I. 6,12-54,60 #102002c android:id/home}
10-01 14:53:58.655: E/FragmentManager(1048):                 android.widget.LinearLayout{418538d8 V.E..... ......I. 60,0-118,72}
10-01 14:53:58.674: E/FragmentManager(1048):                   android.widget.ImageView{41853cc8 G.ED.... ......I. 0,0-0,0 #1020252 android:id/up}
10-01 14:53:58.674: E/FragmentManager(1048):                   android.widget.LinearLayout{41854058 V.E..... ......I. 0,17-46,54}
10-01 14:53:58.684: E/FragmentManager(1048):                     android.widget.TextView{41854450 V.ED.... ......I. 0,0-46,37 #102025a android:id/action_bar_title}
10-01 14:53:58.694: E/FragmentManager(1048):                     android.widget.TextView{41855178 G.ED.... ......I. 0,0-0,0 #102025b android:id/action_bar_subtitle}
10-01 14:53:58.705: E/FragmentManager(1048):               com.android.internal.view.menu.ActionMenuView{418a5e30 V.ED.... ......I. 480,0-480,72}
10-01 14:53:58.714: E/FragmentManager(1048):             com.android.internal.widget.ActionBarContextView{418557b8 G.E..... ......ID 0,0-0,0 #1020341 android:id/action_context_bar}
10-01 14:53:58.714: E/FragmentManager(1048):           android.widget.ImageView{41855d28 V.ED.... ......I. 480,0-480,24}
10-01 14:53:58.734: E/FragmentManager(1048):         com.android.internal.widget.ActionBarContainer{418560a0 G.ED.... ......ID 0,0-0,0 #1020342 android:id/split_action_bar}
10-01 14:53:58.734: D/AndroidRuntime(1048): Shutting down VM
10-01 14:53:58.734: W/dalvikvm(1048): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-01 14:53:58.804: E/AndroidRuntime(1048): FATAL EXCEPTION: main
10-01 14:53:58.804: E/AndroidRuntime(1048): java.lang.IllegalArgumentException: No view found for id 0x7f090001 (com.example.test:id/content_frame) for fragment WordFragment{418ae390 #0 id=0x7f090001}
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.os.Handler.handleCallback(Handler.java:730)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.os.Looper.loop(Looper.java:137)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at java.lang.reflect.Method.invoke(Method.java:525)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-01 14:53:58.804: E/AndroidRuntime(1048):     at dalvik.system.NativeStart.main(Native Method)
10-01 14:54:01.205: I/Process(1048): Sending signal. PID: 1048 SIG: 9

My Activity File :

package com.example.test;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Home extends Activity implements OnClickListener {
Button btn1;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    FrameLayout frame = (FrameLayout) findViewById(R.id.content_frame);
    btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

        Fragment newFragment = new WordFragment();
        FragmentTransaction transaction = getFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.content_frame, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();

        break;

 }
 }

My WordFragment class:

package com.example.test;

import android.app.Fragment;
import android.app.FragmentTransaction;   
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class WordFragment extends Fragment {

public WordFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

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

    return view;
}
}

原文:https://stackoverflow.com/questions/26148890
更新时间:2019-07-12 10:31

最满意答案

当您使用片段事务在onClick中添加WordFragment时,它抱怨它找不到具有用于放置片段的id content_frame的父视图。

检查以确保home.xml布局中有一个id为content_frame的容器作为此片段的父级。


When you add your WordFragment in onClick using the fragment transaction, it is complaining that it can't find a parent view with the id content_frame in which to place your fragment.

Check to make sure your home.xml layout has a container with an id of content_frame to serve as the parent for this fragment.

2014-10-01

相关问答

更多

选中android片段中的内容?(Tabs content in Android fragments?)

下面是编辑过的代码,我在其中给出了一个样本,包括Fragment Tabs中的Activity。 如果您无法理解,请告诉我: //FragmentActivity having tabs package com.example.tabhost; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment ...

Facebook登录android.View.InflateException错误(Facebook Login android.View.InflateException error)

com.alexlamond.fblogin.SelectionFragment不是android.support.v4.app.Fragment 引起:java.lang.ClassCastException:com.alexlamond.fblogin.SelectionFragment无法强制转换为android.support.v4.app.Fragment 检查SelectionFragment扩展的Fragment是否来自支持库(android.support.v4.app)。 com ...

如何设置动态高度来查看android中的分页器子片段?(How to set dynamic height to view pager child fragments in android?)

最后,我找到答案。 将下面的属性放在Webview的xml中。 这将根据内容自动采用webview高度。 android:scrollbarStyle="insideOverlay" Finally, I find the answer. Place below property in Webview in xml. This will take webview height automatically based on the content. android:scrollbarStyle=" ...

选择语句的SQLite Android Logcat错误(SQLite Android Logcat error for select statement)

这是在Adapter类中,我甚至没有选择“_id”(KEY_ROWID)列 CursorAdapter及其子类要求 Cursor有一个名为_id的列。 如果您有自己的,请使用自己的,或者切换到rawQuery()以便在列表列表中包含ROWID AS _id以返回。 This is in the Adapter class and i'm not even selecting the "_id"(KEY_ROWID) column CursorAdapter and its subclasses r ...

无法在android studio中加载任何外部字体(Can't load any external fonts in android studio)

按照下面链接中的说明进入清单文件并在应用程序xml标记下添加android:name =“the-below-implementation” https://github.com/chrisjenx/Calligraphy Follow the instructions in the link below and go to the manifest file and add android:name="the-below-implementation" under application xml ...

碎片不兼容的类型(Fragments Incompatible types)

在您的类EmployeeDetails声明中,执行以下操作: public class EmployeeDetails extends Fragment { ...... 并使用import android.app.Fragment; 不使用import android.support.v4.app.Fragment; In your class EmployeeDetails declaration, do this: public class EmployeeDetails extends ...

如何使用片段在Web视图中向后导航?(How do I navigate backwards in a Web View using fragments?)

使用这个mWebView应该是你的webview 你必须申报 private WebView mWebView; 在onCreate()方法中初始化它,你应该很高兴。 mWebView = (WebView) findViewById(R.id.webView); 然后使用下面的代码进行活动 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getAction() == KeyEven ...

在Fragments中面对GetJson()和OnClick()中的错误。(Facing Error in GetJson() & OnClick() in Fragments. Plese see code help me for same)

因为它说“NetworkOnMainThreadException”没有命中api direclty而是使用AsyncTask。 首先定义asynctask class Calculate extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); } ...

相关文章

更多

最新问答

更多
  • Unity着色器错误;(Unity shader error; presumably in if statement)
  • 如何在Liferay portlet中设置Cookie?(How to set a Cookie in Liferay portlet?)
  • C#只读访问List <>或其他集合,或ToArray()魔术(C# readonly access to List<> or other collection, or ToArray() magic)
  • 如何使用Google Web工具包创建登录应用程序?(How to create a login application using Google web tool kit? [closed])
  • 如何使我的函数返回结果作为全局变量?(How to make my function return results as a global variable?)
  • 为什么我的网站需要“启用32位应用程序”?(Why does my website need “Enable 32-bit applications”?)
  • 红宝石数组具有相同的值(ruby array of hash with same value)
  • Android(在Scala中):StackOverflowError取决于何时启动线程?(Android (in Scala): StackOverflowError depends on when to start a thread?)
  • 适用于iOS应用的通用链接(Universal Links for iOS apps)
  • 创建并打开文件linux编程(create and open file linux programming)
  • Datepicker,第2个日期是从第1个日期开始的X天(Datepicker, 2nd date is X days from 1st date)
  • 背景大小过渡不起作用(Transition on background-size doesn't work)
  • 在React / Redux中的On Click函数中传递Prop(Passing a Prop in an On Click Function in React/Redux)
  • 关键字在代码中做了什么,是否有没有此关键字的替代方法?(what does the keyword this does in the code and are there any alternate methods without this keyword? [duplicate])
  • 反向设计FoxPro / dBsae数据库以创建EER模型(Reverse Engineer a FoxPro/dBsae database to create the EER model)
  • 在R中的变量中组合具有相同值的行(Combine rows that have same value in a variable in R [duplicate])
  • SQL Server:非空唯一主键(SQL Server: Non-null unique vs. Primary Key)
  • 将扩展ASCII字符代码转换为ISO-8859-1(Convert extended ASCII character codes to ISO-8859-1)
  • 如何在.NET中读取m4a文件中的标签?(How to read tags out of m4a files in .NET?)
  • 呼叫链接的性能提升?(Performance gain on call chaining?)
  • 风景名胜区规划设计文本里面都包含哪些内容?推荐一家旅游规划设计公司?谢谢!
  • 如何在String中将字符串转换为枚举?(How do I convert a string to enum in TypeScript?)
  • SQLite SELECT出现异常如何解决它?(SQLite SELECT gives exception How to fix it?)
  • NSLog不会从ViewController类输出,而是来自AppDelegate类的WILL。(NSLog will NOT output from the ViewController class, but WILL from the AppDelegate class. iOS:Objective C)
  • 更改UIPicker突出显示的标签宽度(change UIPicker highlighted tab width)
  • 空心倒五角形(Hollow inverted pentagon)
  • 如何设置仅使用OpenID的Plone站点(How to setup Plone sites working only with OpenID)
  • Paperclip缺少Amazon S3的协议(https)(Paperclip is missing the Protocol (https) with Amazon S3)
  • 从宏中部署netbeans中的ANT文件(Deploy ANT file in netbeans from macro)
  • XMLReader是未知的(XMLReader is unknown)