【问题标题】:MY fragments would not show我的片段不会显示
【发布时间】: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


【解决方案1】:

试试这个

<?xml version="1.0" encoding="utf-8"?>
<fragment
android:layout_width="350dp"
android:layout_height="70dp"
android:name="com.example.yourApp.FragmentA"
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="com.example.yourApp.FragmentB"
android:id="@+id/fragment2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<fragment
android:layout_width="350dp"
android:layout_height="70dp"
android:name="com.example.yourApp.FragmentC"
android:id="@+id/fragment3"
android:layout_below="@+id/fragment2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />

【讨论】:

  • 它不起作用,我在使用这个之后是否对 gallery.java 进行任何更改?
  • 它给出了什么错误,您发布画廊布局的布局是什么?
  • 这些片段在什么布局中。您能否发布您的完整画廊布局?
猜你喜欢
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
  • 1970-01-01
相关资源
最近更新 更多