我第一次在标签上实现了带有滑动功能的Actionbar。 我在蜂窝OS(Api等级11)上运行。 因此,我必须使用支持兼容性库v13来使用视图寻呼机和用于构建滑动机制的其他组件,如FragmentPagerAdapter。
我正在使用developer.android上的教程来构建它。 Here is the link.
然而,我得到一个奇怪的问题,没有找到像附加和分离这样的方法。
让我发布完整的跟踪记录
09-04 13:07:42.560: E/AndroidRuntime(774): java.lang.NoSuchMethodError: android.app.FragmentTransaction.attach
09-04 13:07:42.560: E/AndroidRuntime(774): at android.support.v13.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
09-04 13:07:42.560: E/AndroidRuntime(774): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
09-04 13:07:42.560: E/AndroidRuntime(774): at android.support.v4.view.ViewPager.populate(ViewPager.java:1016)
09-04 13:07:42.560: E/AndroidRuntime(774): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-04 13:07:42.560: E/AndroidRuntime(774): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
09-04 13:07:42.560: E/AndroidRuntime(774): at android.os.Handler.handleCallback(Handler.java:587)
09-04 13:07:42.560: E/AndroidRuntime(774): at android.os.Handler.dispatchMessage(Handler.java:92)
09-04 13:07:42.560: E/AndroidRuntime(774): at android.os.Looper.loop(Looper.java:126)
09-04 13:07:42.560: E/AndroidRuntime(774): at android.app.ActivityThread.main(ActivityThread.java:3997)
09-04 13:07:42.560: E/AndroidRuntime(774): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 13:07:42.560: E/AndroidRuntime(774): at java.lang.reflect.Method.invoke(Method.java:491)
09-04 13:07:42.560: E/AndroidRuntime(774): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-04 13:07:42.560: E/AndroidRuntime(774): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-04 13:07:42.560: E/AndroidRuntime(774): at dalvik.system.NativeStart.main(Native Method)
我也经历了代表相同问题的相同链接,但它没有解决我的问题。 this is the link to same issue
我没有使用ActionbarSherloc,因为我在Api等级11上运行。
我发布了我的Adapter类和TabListener,以防你想看看。
public class MyPagerAdapter extends FragmentPagerAdapter{
Fragment mFragment;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public android.app.Fragment getItem(int position) {
// TODO Auto-generated method stub
switch (position) {
case 0:
mFragment = (PatientForms)new PatientForms();
return mFragment;
case 1:
mFragment = (Vitals)new Vitals();
return mFragment;
case 2:
mFragment = (ChartsActivity)new ChartsActivity();
return mFragment;
case 3:
mFragment = (Note)new Note();
return mFragment;
default:
return null;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
// TODO Auto-generated method stub
}
}
private class MyTabListener implements ActionBar.TabListener
{
private Fragment mFragment;
private final Activity mActivity;
private final String mFragName;
public MyTabListener( Activity activity, String fragName )
{
mActivity = activity;
mFragName = fragName;
}
@Override
public void onTabSelected( Tab tab, FragmentTransaction ft )
{
Log.i(TAG, "tab position to set is "+tab.getPosition());
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
// ft.remove( mFragment );
// mFragment = null;
}
}
答案 0 :(得分:0)
试试这个
public android.support.v4.app.Fragment getItem(int position) {
// TODO Auto-generated method stub
switch (position) {
default:
return null;
}
}
<强>更新强>
检查导入语句并替换此行
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.FragmentActivity;
替换为
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;