我正在尝试使用支持库编写tabListener。但是,我不确定我是否走在正确的轨道上。你能检查我的代码并给我反馈吗?
SimpleTabListener.java
public class SimpleTabListener implements ActionBar.TabListener {
Context _context;
String _tabFragmentClassName;
Fragment _tabFragment = null;
FragmentTransaction _ft;
public SimpleTabListener(Context context, String tabFragmentClassName)
{
_context = context;
_tabFragmentClassName = tabFragmentClassName;
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
if(_tabFragment==null)
{
_tabFragment = Fragment.instantiate(_context, _tabFragmentClassName);
ActionBarActivity mActivity = (ActionBarActivity) _context;
android.support.v4.app.FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
_ft=ft;
_ft.add(android.R.id.content, _tabFragment);
_ft.commit();
}
else
_ft.attach(_tabFragment);
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
_ft.detach(_tabFragment);
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener1 = new SimpleTabListener(this, "com.example.actionbartabbednav_p5.fragment1");
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("Blablaaaa");
tab1.setTabListener(tabListener1);
actionBar.addTab(tab1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
logcat的
07-10 15:04:45.210:E / AndroidRuntime(5695):致命异常:主
07-10 15:04:45.210:E / AndroidRuntime(5695):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.actionbartabbednav_p5 / com.example.actionbartabbednav_p5.MainActivity}:android.support。 v4.app.Fragment $ InstantiationException:无法实例化片段com.example.actionbartabbednav_p5.fragment1:确保类名存在,是公共的,并且具有公共的空构造函数
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.app.ActivityThread.access $ 700(ActivityThread.java:140)
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1237)
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.os.Handler.dispatchMessage(Handler.java:99)
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.os.Looper.loop(Looper.java:137)
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.app.ActivityThread.main(ActivityThread.java:4921)
07-10 15:04:45.210:E / AndroidRuntime(5695):at java.lang.reflect.Method.invokeNative(Native Method)
07-10 15:04:45.210:E / AndroidRuntime(5695):at java.lang.reflect.Method.invoke(Method.java:511)
07-10 15:04:45.210:E / AndroidRuntime(5695):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-10 15:04:45.210:E / AndroidRuntime(5695):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-10 15:04:45.210:E / AndroidRuntime(5695):at dalvik.system.NativeStart.main(Native Method)
07-10 15:04:45.210:E / AndroidRuntime(5695):引起:android.support.v4.app.Fragment $ InstantiationException:无法实例化片段com.example.actionbartabbednav_p5.fragment1:确保类名exists,是public,并且有一个空的构造函数是公共的
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.support.v4.app.Fragment.instantiate(Fragment.java:411)
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.support.v4.app.Fragment.instantiate(Fragment.java:379)
07-10 15:04:45.210:E / AndroidRuntime(5695):at com.example.actionbartabbednav_p5.SimpleTabListener.onTabSelected(SimpleTabListener.java:33)
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.support.v7.app.ActionBarImplICS $ TabWrapper.onTabSelected(ActionBarImplICS.java:558)
07-10 15:04:45.210:E / AndroidRuntime(5695):at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:587)
07-10 15:04:45.210:E / AndroidRuntime(5695):at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:516)
07-10 15:04:45.210:E / AndroidRuntime(5695):at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:502)
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.support.v7.app.ActionBarImplICS.addTab(ActionBarImplICS.java:247)
07-10 15:04:45.210:E / AndroidRuntime(5695):在android.support.v7.app.ActionBarImplJB.addTab(ActionBarImplJB.java:20)
Fragment1.java
package com.example.actionbartabbednav_p5;
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, container, false);
Button btn = (Button) view.findViewById(R.id.button1);
return view;
}
}