我正试图在eclipse中添加滑动标签,但有些东西不起作用

时间:2015-05-29 00:24:12

标签: android eclipse tabs

我正在尝试按照谷歌的一个指南在我的应用程序中添加滑动标签,但是当我尝试启动应用程序时出现此错误:

05-29 02:04:40.353: E/AndroidRuntime(21092): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
05-29 02:50:06.212: E/AndroidRuntime(3506):     at com.tabs.dusandimitrijevic.Tab1.onCreate(Tab1.java:33)

以下是我的一个片段标签:

package com.tabs.dusandimitrijevic;

import com.dusandimitrijevic.spisakzakupovinu.R;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by hp1 on 21-01-2015.
 */
//In this case, the fragment displays simple text based on the page
public class Tab1 extends Fragment {
 public static final String ARG_PAGE = "ARG_PAGE";

 private int mPage;

 public static Tab1 newInstance(int page) {
     Bundle args = new Bundle();
     args.putInt(ARG_PAGE, page);
     Tab1 fragment = new Tab1();
     fragment.setArguments(args);
     return fragment;
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     mPage = getArguments().getInt(ARG_PAGE);
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.tab_1, container, false);
     TextView textView = (TextView) view;
     textView.setText("Fragment #" + mPage);
     return view;
    }
}

这是我的FragmentPagerAdapter:

package com.tabs.dusandimitrijevic;

import android.R;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

/**
 * Created by hp1 on 21-01-2015.
 */
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
    private Context context;

    public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public Fragment getItem(int position) {
         switch (position) {

         case 0:
         return new Tab1();

         case 1:
         return new Tab2();

         case 2:
         return new Tab3();
         }

         return null;
         }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}

这是我的MainActivity:

package com.dusandimitrijevic.spisakzakupovinu;

import java.util.ArrayList;

import com.tabs.dusandimitrijevic.SampleFragmentPagerAdapter;
import com.tabs.dusandimitrijevic.SlidingTabLayout;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

        // Get the ViewPager and set it's PagerAdapter so that it can display items
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(), 
            MainActivity.this));

        // Give the SlidingTabLayout the ViewPager
        SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
        // Center the tabs in the layout
        slidingTabLayout.setDistributeEvenly(true);
        slidingTabLayout.setViewPager(viewPager);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            Toast.makeText(MainActivity.this, "Ovo su podešavanja", Toast.LENGTH_SHORT).show();
            return true;
        }

        if(id == R.id.navigate){
            Intent i= new Intent(MainActivity.this, SubActivity.class);
            startActivity(i);
        }

        return super.onOptionsItemSelected(item);
    }
}

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您已经编写了静态工厂函数来创建片段,但是您不能使用它?!

您的适配器应使用它创建片段:

@Override
public Fragment getItem(int position) {
     return Tab1.newInstance(position);
 }