2012-12-01 118 views
1

我剛開始學習Android Fragments。請幫助我爲什麼我的後續程序崩潰。Android Fragments崩潰

一個活動包含兩個片段,每個片段只有一個顯示按鈕。 我試圖按照在頭第一個Android發展的例子... BUT :(

活動包含片段

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> 
+1

您正在使用哪個版本的Android?我問你這個問題,因爲如果你沒有測試你的代碼3.x或更高,你需要從支持包擴展FragmentActivity類而不是普通的Activity類。 – Jorge

+0

發佈您的LogCat錯誤,以便我們可以看到發生了什麼。 – Sam

+0

難道你不打擾那兩個按鈕有相同的ID嗎?我認爲你應該改變它 – wasyl

回答

0

您應該包括支持庫發現here然後進行Activity延伸FragmentActivity

只有Android 3.x及以上版本將FragmentManagerFragment支持添加到基類Activity類中。

+0

夥計們,我瞄準所有版本,從2.3到4.1 ...沒有任何工作... :(@ wasyl問題沒有重新按鈕編號..我只是創建佈局與單個目標在其中進行測試 – abidkhan303

+0

我也爲這個項目添加了支持自由度.....有人可以在日食中複製此代碼並運行...您將看到問題 – abidkhan303

+0

崩潰日誌對我們可能很有用。 。不是每個人都有時間來重現你的問題,但如果你能給我們提供我們需要的所有線索,我們可能有想法來解決它。 – Cata

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; 
    } 
}