在我的应用中,我有一个活动和两个片段。我正在尝试做的是使用按钮在这两个片段之间或前面之间导航。每个导航按钮都以片段形式定义。
第一个问题是,使用下面的代码,我在应用程序运行时得到异常:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{...MainActivity}:
java.lang.IllegalArgumentException: No view found for id 0x7f0c0050 for fragment Fragment1{b41da690 #0 id=0x7f0c0050}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c0050 for fragment Fragment1{b41da690 #0 id=0x7f0c0050}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:823)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
at android.app.Activity.performStart(Activity.java:5017)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
CODE:
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set desired fragment for the first time
setFragment(this, new Fragment1());
}
public void switch_fragment(View view, String fragmentID) {
Fragment newFragment = null;
switch (fragmentID) {
case Fragment1.ID:
newFragment = new Fragment1();
break;
case Fragment2.ID:
newFragment = new Fragment2();
break;
}
setFragment(this, newFragment);
}
public static void setFragment(Activity activity, Fragment fragment) {
FragmentTransaction fragmentTransaction = activity.getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_placeHolder, fragment);
fragmentTransaction.commit();
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<FrameLayout
android:id="@+id/fragment_placeHolder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment1"
/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment2"
/>
</RelativeLayout>
Fragment1.java
public class Fragment1 extends Fragment {
public static final String ID = "1";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
fragment1.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO TO FRAGMENT 1"
android:id="@+id/btn_1"
android:onClick="switch_fragment"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Fragment2.java
public class Fragment2 extends Fragment {
public static final String ID = "2";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
fragment2.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO TO FRAGMENT 2"
android:id="@+id/btn_2"
android:onClick="switch_fragment"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
如果此示例是实现此目的的错误方法,请您为新手链接一些类似的示例。
答案 0 :(得分:1)
你错过了一个非常重要的部分是setContentView
,在使用替换之前你应该有一个现有的片段。所以在onCreate
更改:
// set desired fragment for the first time
setFragment(this, new Fragment1());
应改为:
setContentView(R.layout.main_activity);//then
// set desired fragment for the first time
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// The id specified here identifies which ViewGroup to
// append the Fragment to.
ft.add(R.id.fragment_placeHolder, new Fragment1());
ft.commit();
尝试替换:
... activity.getFragmentManager()
使用:
... getSupportFragmentManager()
并删除:
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment1"
/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment2"
/>
最后,将switch_fragment
更改为:
public void switch_fragment(View view) {
Fragment newFragment = null;
switch (view.getID()) {
case R.id.btn_2:
newFragment = new Fragment1();
break;
case R.id.btn_1:
newFragment = new Fragment2();
break;
}
setFragment(this, newFragment);
}
答案 1 :(得分:0)
从活动布局文件
中删除以下代码<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment1"
/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment2"
/>
另请在退货声明之前在您的片段中使用以下内容
super.onCreateView(inflater, container, savedInstanceState);
并尝试运行您的代码。