我目前正在为学校目的创建一个Android应用程序。我的第一个问题是,我的导航抽屉出了问题。当我点击包含tablayout的“Home”时,应用程序崩溃了。这是我的导航抽屉的代码:
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;
}
}
答案 0 :(得分:0)
对于错误( NavigationDrawer无法转换为android.support.v7.app.ActionBarActivity ),请在此处进行修改:
final ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
作为
final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
可能会有效。
(或者可能不需要施法:
final ActionBar actionBar = getActivity().getSupportActionBar();
)
因为您的Activity(NavigationDrawer)不是ActionBarActivity而是AppCompatActivity。
对于TabLayout问题:
你应该将TabLayout置于应放置的位置。例如:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="@+id/tabspager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
在Java代码中,您可以将ViewPager设置为TabLayout。例如:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
ViewPager viewPager = (ViewPager) findViewById(R.id.tabspager);
tabLayout.setupWithViewPager(viewPager);
如需进一步使用或指导,请搜索StackOverflow Q&amp; As及其他信息。