我正在尝试从活动切换到片段。我确实认为我的代码是正确的。但我仍然在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;
}
}
答案 0 :(得分:0)
从检查错误输出来看,我觉得这一行:
transaction.replace(R.id.content_frame, newFragment);
发出错误。您确定您有" content_frame"?
的布局文件答案 1 :(得分:0)
当您使用片段事务在onClick中添加WordFragment时,它抱怨它无法找到ID为content_frame的父视图来放置片段。
检查以确保home.xml布局中有一个id为content_frame的容器作为此片段的父级。