无法开始活动。引起:android.view.InflateException:

时间:2015-07-01 11:06:04

标签: java android pagerslidingtabstrip

LogCat错误:
https://github.com/Heromine/tempapp1/issues/2

任何人都可以告诉我,为什么我会收到此错误? 我不明白我做错了什么:

MainActivity.java:

       public class MainActivity extends ActionBarActivity {

    // UI elements
    public static Toolbar toolbar;
    public static ViewPager viewPager;
    private AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    // Tab titles
    private String[] tabs = {"Home", "Note"};


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        SharedPreferences settings = getSharedPreferences("prefs", 0);
        boolean firstRun = settings.getBoolean("firstRun", true);
        if (firstRun) {  // here run your first-time instructions, for example :
            startActivity(new Intent(this, MyIntro.class));
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("firstRun", false);
            editor.commit();
        }

        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        // Set up the action/tool bar.
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar); // Setting toolbar with setSupportActionBar()

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(mAppSectionsPagerAdapter);
      /*  ViewPager.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            swipeLayout.setEnabled(false);
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    swipeLayout.setEnabled(true);
                    break;
            }
            return false;
        }
    });
    */

        // Adding Tabs
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabs.setViewPager(viewPager);
        tabs.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between different app sections

                // change toolbar title
                if (position == 0)
                    toolbar.setTitle(getResources().getString(R.string.app_name));
            }
        });
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.action_settings:
                startActivity(new Intent("android.intent.action.SettingsActivity"));
                return true;

            case R.id.action_donate:
                startActivity(new Intent("android.intent.action.DonateActivity"));
                return true;

            case R.id.action_rate:
                Intent i2 = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.service.beta.servicenotesbeta"));
                startActivity(i2);


                return super.onOptionsItemSelected(item);

            default:
                return super.onOptionsItemSelected(item);

        }


    }

    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public android.support.v4.app.Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new Fragment_main();
                case 1:
                    return new Fragment_second();
            /*case 2:
                return new Fragment_third();
            case 3:
                return new Fragment_fourth();*/
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 2; //3-4
        }

    }

}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
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"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/ToolBarStyle"
    android:id="@+id/toolbar"/>

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true" />

 </RelativeLayout>

 </LinearLayout>

请告诉我我做错了什么 我把问题链接,因为当我把问题代码放到它时,它给了我一个错误

3 个答案:

答案 0 :(得分:1)

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
at com.astuetz.PagerSlidingTabStrip.notifyDataSetChanged(PagerSlidingTabStrip.java:200)
at com.astuetz.PagerSlidingTabStrip.setViewPager(PagerSlidingTabStrip.java:182)
at com.service.beta.servicenotesbeta.MainActivity.onCreate(MainActivity.java:67)

<com.astuetz.PagerSlidingTabStrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="48dip" />

出现问题

您是否看过https://github.com/astuetz/PagerSlidingTabStrip/了解更多信息?

答案 1 :(得分:1)

转到AppSectionsPagerAdapter.java并添加这段代码,以提供PagerSlidingTabStrip可以获得的标题。

@Override
public CharSequence getPageTitle(int position) {
    if (position == 0)
    {
        return "Movies";
    }
    if (position == 1)
    {
        return "Events";
    }
    return null;
}

答案 2 :(得分:0)

我修复了,问题是我没有将标题添加到标签