如何使用theme.holo.light.darkactionbar在活动中添加制表符布局

时间:2016-11-04 11:42:03

标签: java android android-tablayout

我通过自定义Actionbar添加菜单图标,并使用Activity扩展我的课程。这工作正常但是当我在xml文件中添加Tablayout时,android studio会因使用appcompat主题而引发异常。在我的项目自定义操作栏菜单图标中使用Appcompat主题后,它会将java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference

作为例外

这是我的代码:

package com.dzirecreatives.showvoo;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.dzirecreatives.showvoo.Fragments.CreateEventsFrag;
import com.dzirecreatives.showvoo.Fragments.TicketsFrag;
import com.dzirecreatives.showvoo.Fragments.TrendingFrag;
import com.dzirecreatives.showvoo.adapter.TitleNavigationAdapter;

public class TrendingTicketsEvents extends Activity implements ActionBar.OnNavigationListener {
    ImageView search_action;
    // action bar
    private ActionBar actionBar;
    private TitleNavigationAdapter titleNavigationAdapter;

    TabLayout tab_trending_tickets_events;
    ViewPager viewPager_trending_tickets_events;

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

        actionBar = getActionBar();
        //actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        titleNavigationAdapter = new TitleNavigationAdapter(getApplicationContext());
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#454545")));
        actionBar.setListNavigationCallbacks(titleNavigationAdapter, this);

//        viewPager_trending_tickets_events= (ViewPager) findViewById(R.id.viewPager_trending_tickets_events);
//        //viewPager_trending_tickets_events.setAdapter(new CustomAdapter(this.getSupportFragmentManager(),getApplicationContext()));
//        tab_trending_tickets_events= (TabLayout) findViewById(R.id.tab_trending_tickets_events);
//        tab_trending_tickets_events.setupWithViewPager(viewPager_trending_tickets_events);
//        tab_trending_tickets_events.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                viewPager_trending_tickets_events.setCurrentItem(tab.getPosition());
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//                viewPager_trending_tickets_events.setCurrentItem(tab.getPosition());
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//                viewPager_trending_tickets_events.setCurrentItem(tab.getPosition());
//            }
//        });
    }



    private class CustomAdapter extends FragmentPagerAdapter {
        private String fragment[]={"Trending","Tickets","Create Events"};

        public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
            super(supportFragmentManager);
        }

        @Override
        public int getCount() {
            return fragment.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new TrendingFrag();
                case 1:
                    return new TicketsFrag();
                case 2:
                    return new CreateEventsFrag();
                default:
                    return null;
            }
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return fragment[position];
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search_menu_action, menu);
        RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.search_action).getActionView();
        search_action = (ImageView) badgeLayout.findViewById(R.id.img_search);
        search_action.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Choosecity();
            }
        });
        return true;
    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        return false;
    }
}

0 个答案:

没有答案