Java 从扩展片段的类调用方法
我有Java 从扩展片段的类调用方法,java,android,methods,android-fragments,Java,Android,Methods,Android Fragments,我有StartActivity课程。它从片段扩展: StarActivity.java的代码如下所示: public class StartActivity extends Fragment { public static Context appContext; ActionBar actionbar; int state = 0; /** Called when the activity is first created. */ @Override
StartActivity
课程。它从片段
扩展:
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中,我声明了一个变量:fragmentfragment1=newstartactivity()代码>
如何调用drawTab
方法。我尝试了fragment.drawTab
,但它不能。如果我在StartActivity.java的onCreateView
中调用drawTab
,它运行正常。但当我没有在onCreateView中调用drawTab()
时,会发生错误:(
运行错误:
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;
}
问题是,当您创建一个片段的instace时,它还没有附加到活动。因此,在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
}
初始化片段时使用布尔标志:
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();如果我在StartActivity.java的onCreateView中调用drawTab,它运行正常。但是当我在onCreateView中不调用drawTab()时,会发生错误,没有通知。仅:E/():设备已断开连接:1“您是否尝试过放置((StartActivity)fragment1).drawTab();替换后(在您的swithc案例语句中)?我尝试过,但不起作用。我使用了StartActivity fragment1=new StartActivity();fragment1.drawTab();
但也不起作用。我不知道调试的原因,请观看“actionbar”变量错误为:评估期间的错误