美好的一天!我目前正在爲學校目的創建一個Android應用程序。我的第一個問題是,我的導航抽屜出現問題。當我點擊包含tablayout的'Home'時,該應用程序崩潰。這是我爲我的抽屜式導航代碼:在工具欄中添加tablayout
public class NavigationDrawer extends AppCompatActivity{
private static String tag = NavigationDrawer.class.getSimpleName();
private DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;
private ActionBarDrawerToggle DrawerToggle;
private CharSequence mTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
mTitle="iGPH";
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawer = (DrawerLayout)findViewById(R.id.drawer_layout);
// Find our drawer view
nvDrawer = (NavigationView)findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
//Circular Image
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo);
Bitmap circularBitmap = ImageConverter.getRoundedCornerBitmap(bitmap, 100);
ImageView circularImageView = (ImageView)findViewById(R.id.avatar);
circularImageView.setImageBitmap(circularBitmap);
DrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawer, /* DrawerLayout object */
toolbar,/* nav drawer icon to replace 'Up' caret */
R.string.open, /* "open drawer" description */
R.string.close /* "close drawer" description */
){
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mTitle);
}
};
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
mDrawer.setDrawerListener(DrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the planet to show based on
// position
android.support.v4.app.Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.Home:
fragmentClass = Homepage.class;
break;
case R.id.Blog:
fragmentClass = Fragment_Blog.class;
break;
case R.id.Planner:
fragmentClass = VerificationPage.class;
break;
default:
fragmentClass = VerificationPage.class;
}
try {
fragment = (android.support.v4.app.Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item, update the title, and close the drawer
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
switch (item.getItemId()) {
case android.R.id.home:
mDrawer.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
// Make sure this is the method with just `Bundle` as the signature
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
}
這是我的主頁代碼:
import android.app.ActionBar.Tab;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Homepage extends Fragment implements TabListener {
private ViewPager viewPager;
private ActionBar actionBar;
private TabPagerAdapter tabPagerAdapter;
private String[] tabs = {"Luzon", "Visayas", "Mindanao"};
public Homepage() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.layout_homepage, container, false);
viewPager = (ViewPager) view.findViewById(R.id.tabspager);
tabPagerAdapter = new TabPagerAdapter(getFragmentManager());
viewPager.setAdapter(tabPagerAdapter);
final ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
return view;
}
@Override
public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
@Override
public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
}
和錯誤說: NavigationDrawer不能轉換到Android。 support.v7.app.ActionBarActivity
layout_homepage.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:layout="http://schemas.android.com/apk/res-auto"
android:id="@+id/tabspager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment_Blog">
</android.support.v4.view.ViewPager>
TabPagerAdapter.class
public class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new Fragment_luzon();
case 1:
// Games fragment activity
return new Fragment_Visayas();
case 2:
// Movies fragment activity
return new Fragment_Mindanao();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
謝謝@hata。我試着將它改爲AppCompatActivity。但是現在我遇到了另一個錯誤。該錯誤表示:「此配置中不支持選項卡」。我使用工具欄爲我的Tablayout是它的問題? – bluelover
'actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)'已棄用:[ref](http://stackoverflow.com/q/28901821/3501958)。如果您有TabLayout,請發佈其佈局xml。 – hata
我有很多佈局。我已經發布了其中一些。哪個更好用,操作欄或工具欄?只是一個建議先生:)我非常需要它:) – bluelover