我正在尝试创建一个包含带片段和ViewPager标签的Android应用。 我尝试在线寻找解决方案无济于事。我见过的大多数解决方案只提到错误的进口,但我不认为这是问题所在。如果有人不仅可以解决我的错误,而且还可以解释为什么会发生错误,这将是很好的。我遵循本教程,似乎有责任:http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/
这是我的代码:
package com.example.carlajoyce.ccare;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import static android.support.design.widget.TabLayout.*;
public class MytabActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mytab);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(R.string.string_pending));
tabLayout.addTab(tabLayout.newTab().setText(R.string.string_onprocess));
tabLayout.addTab(tabLayout.newTab().setText(R.string.string_onhold));
tabLayout.addTab(tabLayout.newTab().setText(R.string.string_completed));
tabLayout.setTabGravity(GRAVITY_FILL);
final ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab){
viewPager.setCurrentItem(tab.getPosition());
}
public void onTabUnSelected(TabLayout.Tab tab){
}
public void onTabReSelected(TabLayout.Tab tab){
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
你有一个拼写错误:onTabReSelected
而不是onTabReselected
。