材料设计TabLayout

时间:2015-06-12 17:19:15

标签: java android material-design

我尝试使用支持设计创建一个TabLayout活动,但是我有这个错误:

  

引起:java.lang.NoClassDefFoundError:解析失败:Landroid / support / design / R $ styleable;

这是我的代码:

public class GetListeModelActivity extends FragmentActivity {


SectionsPagerAdapter mSectionsPagerAdapter;
public int RESULT_EDIT_LISTEMODEL=1;

ViewPager mViewPager;
List<Fragment> fragments;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get_liste_model);

    final ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions( ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_TITLE|ActionBar.DISPLAY_SHOW_CUSTOM|ActionBar.DISPLAY_HOME_AS_UP);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    fragments= new Vector<Fragment>();

    Bundle b0;
    //Bundle b1 = savedInstanceState;
    //savedInstanceState.putInt("typeliste", 1);
    if(savedInstanceState==null)
    {
        b0=new Bundle();
    }
    else
    {
     b0= savedInstanceState;
    // b1 = savedInstanceState;
    }
    b0.putInt("typelistemodel", 0); //Your id//Put your id to your next Intent  

    fragments.add(Fragment.instantiate(this,ListeModelFragment.class.getName(),b0));

    b0.putInt("typelistemodel", 1); 
    fragments.add(Fragment.instantiate(this,ListeModelFragment.class.getName(),b0));

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), fragments);


    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);


    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);


}

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

@Override
 public boolean onPrepareOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        MenuInflater inflater = getMenuInflater();
        menu.clear();
        switch (mViewPager.getCurrentItem() )
        {
        case  0 :
            inflater.inflate(R.menu.get_liste_model, menu);  
            break;
        case 1 :
            inflater.inflate(R.menu.get_liste_model1, menu); 
            break;
        }

        return super.onPrepareOptionsMenu(menu);
    }

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:

        onBackPressed();
      //do your action here.
        break;
    case R.id.action_add:

        Intent intent = new Intent(this, EditListeModelActivity.class);
        Bundle b = new Bundle();
        b.putInt("typelistemodel", 0); //Your id
        intent.putExtras(b); //Put your id to your next Intent  
        startActivityForResult(intent, RESULT_EDIT_LISTEMODEL);
        break;


    case R.id.action_add1:

        Intent intent1 = new Intent(this, EditListeModelActivity.class);
        Bundle b1 = new Bundle();
        b1.putInt("typelistemodel", 1); //Your id
        intent1.putExtras(b1); //Put your id to your next Intent    
     startActivityForResult(intent1, RESULT_EDIT_LISTEMODEL);
        break;

    }

    return super.onOptionsItemSelected(item);
}


@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) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

    /**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> fragments;


    public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
        // TODO Auto-generated constructor stub
    }


    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class
        // below).
        //return PlaceholderFragment.newInstance(position + 1);
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return this.fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);  


    if (requestCode == RESULT_EDIT_LISTEMODEL && resultCode==1) {
        if(MyUser.mesListesModeles.size()>0)
        {
            for (Fragment fragment : fragments) {
                fragment.onActivityResult(requestCode, resultCode, data);
            }

        }
    }
}

}

我的布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

</LinearLayout>

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

  1. 确保按照instructions正确包含设计库(如果您使用的是Eclipse而不是Gradle / Android Studio,则更为相关)。

  2. 设计库依赖于AppCompat - 如果您使用的是设计库,请确保使用AppCompatActivityreturn false - 这就是设计库获取样式和主题的方式。