当我运行android选项卡滑动获取错误

时间:2015-02-03 09:46:33

标签: android

package actions.com.mytabswipe;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;


public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    ViewPager viewPager;
    TabAdapter tabAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabAdapter = new TabAdapter(getSupportFragmentManager());
        final ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        viewPager = (ViewPager) findViewById(R.id.viewPAger);
        viewPager.setAdapter(tabAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });
        for (int i = 0; i < tabAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab().setText("Tab " + i).setTabListener(this));
        }
    }
    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }
    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }
    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }
}
  

当我运行时遇到错误02-03 04:40:10.277:   E / AndroidRuntime(2378):致命异常:主02-03 04:40:10.277:   E / AndroidRuntime(2378):进程:actions.com.mytabswipe,PID:2378   02-03 04:40:10.277:E / AndroidRuntime(2378):   java.lang.RuntimeException:无法启动活动   ComponentInfo {actions.com.mytabswipe / actions.com.mytabswipe.MainActivity}:   java.lang.NullPointerException 02-03 04:40:10.277:   E / AndroidRuntime(2378):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)   02-03 04:40:10.277:E / AndroidRuntime(2378):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)   02-03 04:40:10.277:E / AndroidRuntime(2378):at   android.app.ActivityThread.access $ 800(ActivityThread.java:135)02-03   04:40:10.277:E / AndroidRuntime(2378):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)   02-03 04:40:10.277:E / AndroidRuntime(2378):at   android.os.Handler.dispatchMessage(Handler.java:102)02-03   04:40:10.277:E / AndroidRuntime(2378):at   android.os.Looper.loop(Looper.java:136)02-03 04:40:10.277:   E / AndroidRuntime(2378):at   android.app.ActivityThread.main(ActivityThread.java:5017)02-03   04:40:10.277:E / AndroidRuntime(2378):at   java.lang.reflect.Method.invokeNative(Native Method)02-03   04:40:10.277:E / AndroidRuntime(2378):at   java.lang.reflect.Method.invoke(Method.java:515)02-03 04:40:10.277:   E / AndroidRuntime(2378):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)   02-03 04:40:10.277:E / AndroidRuntime(2378):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)02-03   04:40:10.277:E / AndroidRuntime(2378):at   dalvik.system.NativeStart.main(Native Method)02-03 04:40:10.277:   E / AndroidRuntime(2378):引起:java.lang.NullPointerException   02-03 04:40:10.277:E / AndroidRuntime(2378):at   actions.com.mytabswipe.MainActivity.onCreate(MainActivity.java:23)   02-03 04:40:10.277:E / AndroidRuntime(2378):at   android.app.Activity.performCreate(Activity.java:5231)02-03   04:40:10.277:E / AndroidRuntime(2378):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)   02-03 04:40:10.277:E / AndroidRuntime(2378):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)   02-03 04:40:10.277:E / AndroidRuntime(2378):... 11更多

     

My Custom Adapter包actions.com.mytabswipe;

     

import android.support.v4.app.Fragment;进口   android.support.v4.app.FragmentManager;进口   android.support.v4.app.FragmentPagerAdapter;进口   android.support.v4.app.FragmentStatePagerAdapter;

     

/ ** *由Murali于03年2月3日创建。 * / public class TabAdapter   扩展FragmentPagerAdapter {

public TabAdapter(FragmentManager manager) {
    super(manager);

}

@Override
public Fragment getItem(int position) {


    switch (position) {
        case 0:
            new Fragment1();
        case 1:
          new Fragment2();
    }
    return new Fragment1();
}

@Override
public int getCount() {
    return 2;
} } My fragment, package actions.com.mytabswipe;
     

导入android.os.Bundle; import android.support.annotation.Nullable;   import android.support.v4.app.Fragment;进口   android.view.LayoutInflater; import android.view.View;进口   android.view.ViewGroup;

     

/ ** *由Murali于03年2月3日创建。 * / public class Fragment2   扩展片段{

@Override
public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment2,container,false);
} }

2 个答案:

答案 0 :(得分:0)

for'fragment'使用下面的代码来访问操作栏

ActionBar actionBar = ((ActionBarActivity)getActivity()).getSupportActionBar();

答案 1 :(得分:0)

此错误将通过主题获得。所以将manifest xml文件中的主题更改为action