2016-04-22 36 views
0

我想重寫我的活動到對話框,我想以編程方式替換對話框。我試過片段staticaly,它工作正常。當我以編程方式調用片段並用斷點調試它時,我不明白爲什麼它的生命週期從onCreate()開始並結束它...爲什麼從onCreate()和onAttach()不像on靜態調用它?這裏是我的代碼,幫我看看!我的動態放置片段有什麼問題?

base_activity.xml`

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/base_drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 

     android:id="@+id/base_fragment_holder" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </FrameLayout> 

</android.support.v4.widget.DrawerLayout> 

`

BaseActivity.java

public class BaseActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.base_activity); 

     DietsFragment dietsFragment = new DietsFragment(); 
     FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.base_fragment_holder, dietsFragment); 
     fragmentTransaction.commit(); 

    } 
} 

DietsFragment.java

public class DietsFragment extends Fragment 
     implements AdapterView.OnItemClickListener, 
     View.OnClickListener { 
    public FragmentEventListener fragmentEventListener; 

    Cursor diets; 
    DietAdapter adapter; 
    DietHelper helper; 
    ListView listView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     View view = inflater.inflate(R.layout.activity_diets, container, false); 

     listView = (ListView) view.findViewById(R.id.act_diets_lv_diets); 
     if (listView != null) { 
      listView.setOnItemClickListener(this); 
     } 

     FloatingActionButton floatingActionButton = (FloatingActionButton) view.findViewById(R.id.act_diets_fab); 
     if (floatingActionButton != null) 
      floatingActionButton.setOnClickListener(this); 
     return view; 
    } 

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

     setHelper(new DietHelper(getActivity())); 
     setDiets(getHelper().getAllTableRecordsCursor()); 
     setAdapter(new DietAdapter(getActivity(), getDiets())); 
     listView.setAdapter(getAdapter()); 
     fragmentEventListener = (FragmentEventListener) getActivity(); 
    } 

    public void setDiets (Cursor diets) { 
     this.diets = diets; 
    } 

    public void setAdapter (DietAdapter adapter) { 
     this.adapter = adapter; 
    } 

    public void setHelper (DietHelper helper) { 
     this.helper = helper; 
    } 

    public Cursor getDiets() { 
     return diets; 
    } 

    public DietAdapter getAdapter() { 
     return adapter; 
    } 

    public DietHelper getHelper() { 
     return helper; 
    } 
} 
在DietsFragment和BaseActivity

我隱藏其OnClickListener,OnItemClickListener和FragmentEventListener的implementetions,因爲它工作正常,當我在BaseActivity添加片段從靜態XML與

回答

0

我剛剛發現我的錯誤...... 我BaseActivity重載findViewbyId(INT渣油)和我所有的問題,現在我問自己爲什麼我超過了那個方法,我想要用它做什麼?

相關問題