【发布时间】:2015-08-24 19:23:35
【问题描述】:
请帮忙! 我需要在图库页面上显示三个片段,但我的应用程序不断崩溃。 这是gallery.java页面
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class Gallery extends Activity {
Fragment fragmenta;
Fragment fragmentb;
Fragment fragmentc;
FragmentManager fm;
String fraga;
String fragb;
String fragc;
FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
fm = getFragmentManager();
fragmenta=new FragmentA();
fragmentb=new FragmentB();
fragmentc=new FragmentC();
fragmenta=(Fragment)getFragmentManager().findFragmentById(R.layout.fragmenta);
fragmentb=(Fragment)getFragmentManager().findFragmentById(R.layout.fragmentb);
fragmentc=(Fragment)getFragmentManager().findFragmentById(R.layout.fragmentc);
// getFragmentManager().beginTransaction().add(R.id.fragment,fragmenta,fraga).commit();
// getFragmentManager().beginTransaction().add(R.id.fragment2,fragmentb,fragb).commit();
// getFragmentManager().beginTransaction().add(R.id.fragment3,fragmentc,fragc).commit();
ft=fm.beginTransaction();
ft.add(R.id.fragment, new FragmentA());
ft.add(R.id.fragment2, new FragmentB());
ft.add(R.id.fragment3, new FragmentC());
ft.commit();
}
}
这是碎片a。爪哇
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragmenta,container,false);
return v;
}
这是 frag b.java
public class FragmentB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragmentb,container,false);
return v;
}
这是 frag c.java
public class FragmentC extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragmentc,container,false);
return v;
}
请帮忙
共有三个片段,我需要其中三个片段才能显示在图库页面上。 xml代码没问题,所以我没想到在这里添加它
我的画廊.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment
android:layout_width="350dp"
android:layout_height="70dp"
android:name="android.app.DialogFragment"
android:id="@+id/fragment"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
<fragment
android:layout_width="320dp"
android:layout_height="250dp"
android:name="android.app.DialogFragment"
android:id="@+id/fragment2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<fragment
android:layout_width="350dp"
android:layout_height="70dp"
android:name="android.app.DialogFragment"
android:id="@+id/fragment3"
android:layout_below="@+id/fragment2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
【问题讨论】:
-
能否请您发布显示崩溃的
logcat? -
我的逻辑没有显示任何错误,我知道为什么
-
好吧,我想也许是因为你在这里创建了三个片段
fragmenta=new FragmentA();,那么在此处添加ft.add(R.id.fragment, new FragmentA());时,你不需要再次new它们。尝试将这些行更改为ft.add(R.id.fragment, fragmenta);,并对片段 b 和 c 执行相同操作。 -
还是不行。请帮帮我
-
尽可能发布您的活动布局 xml 文件。
标签: android android-fragments fragment android-fragmentactivity android-dialogfragment