1

我刚刚开始学习 Android 片段。请帮助我为什么我的以下程序崩溃了。

一个 Activity 包含两个片段,每个片段只有一个按钮可以显示。我尝试按照 Head First Android Development 中的示例进行操作...但是 :(

包含片段的活动

public class FragmentsContainer extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragmentscontaineractivity);
    }
}

片段容器的布局

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <fragment
            android:name="com.example.nasaimage.ImageOne"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <fragment
            android:name="com.example.nasaimage.ImageTwo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2" />
    </LinearLayout>

第一个片段和布局

 public class ImageOne extends Fragment {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        }

        @Override
        public View onCreateView(LayoutInflater inflater,
                ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.imageoneactivity, container, false);
        }
   }

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

第二个活动和布局

 public class ImageTwo extends Fragment {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        }

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

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

                Button button = (Button) view.findViewById(R.id.button1);
                button.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {

                  }
                });
                return view;
        }
    }


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    </LinearLayout>
4

2 回答 2

4

为什么要使用这种方法,您可以使用更智能的方法...按下按钮、菜单选项或导航抽屉项目后,可以像这样调用片段:

FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            FragmentExcel fragmentS1 = new FragmentExcel();
            fragmentTransaction.replace(R.id.content_frame, fragmentS1);
            fragmentTransaction.commit();

然后片段类可以是这样的,其中有三个具有特定功能的按钮:

public class FragmentExcel extends Fragment {    
    Button buttonCat, buttonBudg, buttonExpense;
    DBHelper db;

    public static Fragment newInstance(Context context) {
        FragmentHistory f = new FragmentHistory();    
        return f;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {    
        if (container == null) {
            return null;
        }

        RelativeLayout mLinearLayout = (RelativeLayout) inflater.inflate(
                R.layout.fragment_excel, container, false);

        db = new DBHelper((getActivity().getBaseContext()));

        buttonCat = (Button) mLinearLayout.findViewById(R.id.buttonCategoies);
        buttonBudg = (Button) mLinearLayout.findViewById(R.id.buttonBudgets);
        buttonExpense = (Button) mLinearLayout
                .findViewById(R.id.buttonExpenses);

        buttonCat.setOnClickListener(new View.OnClickListener() {    
            @Override
            public void onClick(View v) {
                db = new DBHelper((getActivity().getBaseContext()));
                String tableName = "categories";
                db.exportDataIntoCSV(tableName);

                Toast.makeText(getActivity().getBaseContext(),
                        "File is saved in tour SDCard storage",
                        Toast.LENGTH_SHORT).show();    
            }
        });

        buttonBudg.setOnClickListener(new View.OnClickListener() {    
            @Override
            public void onClick(View v) {
                //Do whatever you want    
            }
        });

        buttonExpense.setOnClickListener(new View.OnClickListener() {    
            @Override
            public void onClick(View v) {
                //Do whatever you want              }
        });

        return mLinearLayout;
    }
}
于 2015-06-06T17:54:00.163 回答
0

您应该包括在此处找到的支持库,然后进行Activity扩展FragmentActivity

只有 Android 3.x 及更高版本在基类中添加了FragmentManager和支持。FragmentActivity

于 2012-12-02T01:46:06.900 回答