我想知道是否有人可以帮助我。我对编程很陌生,这有点过头了,但是一旦这个问题得到解决,我想我可以用我现有的技能完成剩下的程序(我希望)。无论如何,我使用Actionbarsherlock创建一个滑动选项卡界面。我使用的代码在Youtube上找到,作为如何操作的示例。如果有人想查看,下载源代码的网址为:https://www.dropbox.com/s/p91dzt4dekuia3q/SwipeyTabs.zip。源代码可以下载,但我会在这里发布我认为相关的部分。基本上,无论何时旋转设备(和模拟器),应用程序都会崩溃。如果我以横向模式启动设备,它将一直工作,直到旋转成纵向,如果我以纵向开始,它将一直工作,直到旋转成横向。我得到一个空指针异常作为错误,如果需要可以提供logcat输出。
MainActivity.java文件是:
package com.rufflez.swipeytabs;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class MainActivity extends SherlockFragmentActivity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Fragment 1"), Fragment_1.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Fragment 2"), Fragment_2.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Fragment 3"), Fragment_3.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Fragment 4"), Fragment_4.class, null);
}
}
TabsAdapter.java文件是:
package com.rufflez.swipeytabs;
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener , ViewPager.OnPageChangeListener{
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final String TAG = "21st Polling:";
static final class TabInfo{
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args){
clss = _class;
args = _args;
}
}
public TabsAdapter(SherlockFragmentActivity fa, ViewPager pager) {
super(fa.getSupportFragmentManager());
mContext = fa;
mActionBar = fa.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args){
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
Log.v(TAG, "clicked");
Object tag = tab.getTag();
for (int i = 0; i<mTabs.size(); i++){
if (mTabs.get(i) == tag){
mViewPager.setCurrentItem(i);
}
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(mContext, "You've deselected a tab", Toast.LENGTH_SHORT).show();
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public int getCount() {
return mTabs.size();
}
}
每个片段_#。java基本上都带有适当的引用:
package com.rufflez.swipeytabs;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragment;
public class Fragment_1 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_1, container, false);
}
}
activity_main.xml是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager" />
</RelativeLayout>
每个片段_#。xml基本上都是这个,改变了相应的参考:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Fragment 1"/>
</LinearLayout>
根据我的想法,做了几个小时的研究,这曾经是几个版本之前解决的ABS中的一个错误,可能这个问题在某种程度上与这个问题有关。任何帮助将非常感谢。特别是如果你能以师生风格提供它,那么我可以更有效地学习。 :-)提前感谢任何试图提供帮助的人!