[英]Call method from class which extends Fragment
I have StartActivity
class. 我有
StartActivity
类。 It extends from Fragment
: 它从
Fragment
扩展:
The code for StarActivity.java
is shown below: 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++;
}
}
In MainClass.java I declare a variable: Fragment fragment1 = new StartActivity();
在MainClass.java中,我声明一个变量:
Fragment fragment1 = new StartActivity();
How to I can call drawTab
method. 如何调用
drawTab
方法。 I try fragment.drawTab
but it can't. 我尝试
fragment.drawTab
但不能。 If I call drawTab
in onCreateView
in StartActivity.java,
it runs OK. 如果我在
StartActivity.java,
onCreateView
中调用drawTab
StartActivity.java,
它将运行正常。 but when I don't call drawTab()
in onCreateView, error happens:( RUN ERROR: 但是当我不在onCreateView中调用
drawTab()
时,发生错误:( RUN ERROR:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container,
false);
return rootView;
}
RUN OK: 运行确定:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container,
false);
drawTab();
return rootView;
}
MainClass.java 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;
}
The problem is when you create an instace of a fragment, it is not attached to the activity yet. 问题是当您创建片段的实例时,它尚未附加到活动中。 So drawing a tab before onAttach method will cause a NPE error.
因此,在onAttach方法之前绘制选项卡将导致NPE错误。
Try to do something below: 尝试执行以下操作:
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
}
Use the boolean flag while initializing your fragment: 在初始化片段时使用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;
}
}
Note: Just an advice, the name of a fragment is annoying. 注意:只是一个建议,片段的名称很烦人。 Why don't you name it StartFragment?
为什么不将其命名为StartFragment?
将其声明为StartActivity Fragment1 = new StartActivity();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.