[英]Call method from class which extends Fragment
我有StartActivity
類。 它從Fragment
擴展:
StarActivity.java
的代碼如下所示:
public class StartActivity extends Fragment {
public static Context appContext;
ActionBar actionbar;
int state = 0;
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container,
false);
return rootView;
}
// call draw Tab method
public void drawTab(){
//ActionBar
actionbar = getActivity().getActionBar();
if(state == 0){
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab PlayerTab = actionbar.newTab().setText("Fragment A");
ActionBar.Tab StationsTab = actionbar.newTab().setText("Fragment B");
Fragment PlayerFragment = new AFragment();
Fragment StationsFragment = new BFragment();
PlayerTab.setTabListener(new MyTabsListener(PlayerFragment));
StationsTab.setTabListener(new MyTabsListener(StationsFragment));
actionbar.addTab(PlayerTab);
actionbar.addTab(StationsTab);
state++;
}
}
在MainClass.java中,我聲明一個變量: Fragment fragment1 = new StartActivity();
如何調用drawTab
方法。 我嘗試fragment.drawTab
但不能。 如果我在StartActivity.java,
onCreateView
中調用drawTab
StartActivity.java,
它將運行正常。 但是當我不在onCreateView中調用drawTab()
時,發生錯誤:( RUN ERROR:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container,
false);
return rootView;
}
運行確定:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container,
false);
drawTab();
return rootView;
}
MainClass.java
private void selectItem(int position) {
Toast.makeText(getBaseContext(), Integer.toString(position), 1).show();
Fragment fragment1 = new StartActivity();
switch (position) {
case 5:
break;
case 1:
((StartActivity) fragment1).drawTab();
FragmentManager fragmentManager1 = getFragmentManager();
fragmentManager1.beginTransaction().replace(R.id.content_frame, fragment1).commit();
break;
default:
break;
}
問題是當您創建片段的實例時,它尚未附加到活動中。 因此,在onAttach方法之前繪制選項卡將導致NPE錯誤。
嘗試執行以下操作:
public class StartActivity extends Fragment {
// TODO: add your other/current fields here
boolean mDrawTabWhileInitializing = false;
public StartActivity(boolean drawTabWhileInitializing) {
mDrawTabWhileInitializing = drawTabWhileInitializing;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
if(mDrawTabWhileInitializing) {
drawTab();
}
return rootView;
}
// TODO: add your other/current methods here
}
在初始化片段時使用boolean標志:
private void selectItem(int position) {
Toast.makeText(getBaseContext(), Integer.toString(position), 1).show();
Fragment fragment1 = null;
switch (position) {
case 1:
fragment1 = new StartActivity(true);
FragmentManager fragmentManager1 = getFragmentManager();
fragmentManager1.beginTransaction().replace(R.id.content_frame, fragment1).commit();
break;
default:
break;
}
}
注意:只是一個建議,片段的名稱很煩人。 為什么不將其命名為StartFragment?
將其聲明為StartActivity Fragment1 = new StartActivity();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.