0

I'm trying to add an button icon in the upper right hand side of action bar and I follow this tutorial

Claims1.java

public class Claims1 extends Fragment {
    long fk;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = this.getArguments();
        fk = bundle.getLong("ab");
        View claims = inflater.inflate(R.layout.claims, container, false);

        ActionBar actionBar = getActivity().getActionBar();
        actionBar.setDisplayOptions(actionBar.getDisplayOptions()
                | ActionBar.DISPLAY_SHOW_CUSTOM);
        ImageView imageView = new ImageView(actionBar.getThemedContext());
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setImageResource(R.mipmap.create);
        ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                | Gravity.CENTER_VERTICAL);
        layoutParams.rightMargin = 40;
        imageView.setLayoutParams(layoutParams);
        actionBar.setCustomView(imageView);
        return claims;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity  {

    private String[] mNavigationDrawerItemTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    ActionBarDrawerToggle mDrawerToggle;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTitle = mDrawerTitle = getTitle();
        // get list items from strings.xml
        mNavigationDrawerItemTitles = getResources().getStringArray(R.array.nav_drawer_items);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                R.mipmap.ic_drawer,
                R.string.drawer_open,
                R.string.drawer_close
        )
        {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getSupportActionBar().setTitle(mTitle);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle(mDrawerTitle);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        // 2.1 create ActionBarDrawerToggle
        ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[5];

        drawerItem[0] = new ObjectDrawerItem(R.mipmap.timesheet, "Time Sheet");
        drawerItem[1] = new ObjectDrawerItem(R.mipmap.claims, "Claims");
        drawerItem[2] = new ObjectDrawerItem(R.mipmap.project_icon, "Project");
        drawerItem[3] = new ObjectDrawerItem(R.mipmap.report, "Report");
        drawerItem[4] = new ObjectDrawerItem(R.mipmap.view, "View");



        DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
        mDrawerList.setAdapter(adapter);



    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getSupportActionBar().setTitle(mTitle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }


    public class DrawerItemClickListener implements ListView.OnItemClickListener {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }


        private void selectItem(int position) {

            Fragment fragment = null;

            switch (position) {
                case 0:
                    fragment=new Information();
                    break;
                case 1:
                    fragment=new Claims1();
                    Bundle bundle=new Bundle();
                    bundle.putLong("ab",WorkDetailsTable.ab);
                    fragment.setArguments(bundle);
                    break;

                case 2:
                    fragment=new Project();
                    break;

                case 3:
                    fragment=new Report();
                    break;

                case 4:
                    fragment=new ViewView();
                    break;

                default:
                    break;
            }

            if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);
                setTitle(mNavigationDrawerItemTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);

            } else {
                Log.e("MainActivity", "Error in creating fragment");
            }
        }
    }

However, my app crashed. Do I need to add anything in my claims1.xml?

11-15 13:22:27.384    4199-4199/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.project, PID: 4199
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.app.ActionBar.getDisplayOptions()' on a null object reference
            at com.example.project.project.Claims1.onCreateView(Claims1.java:26)
            at android.app.Fragment.performCreateView(Fragment.java:2220)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)

(Claims1.java:26)

 actionBar.setDisplayOptions(actionBar.getDisplayOptions()
                | ActionBar.DISPLAY_SHOW_CUSTOM);
Community
  • 1
  • 1
John
  • 684
  • 11
  • 35

1 Answers1

0

You are using wrong getActionBar method.

You are using AppCompatActivity so action bar is from supported library. You should use ((AppCompatActivity) getActivity()).getSupportActionBar();

If you not make cast getActionBaris asking about android.app.ActionBar but using AppCompatActivity you need to ask about android.support.v7.app.ActionBar

michal.luszczuk
  • 2,883
  • 1
  • 15
  • 22
  • Did you mean I have to change `getActionBar` to `((AppCompatActivity) getActivity()).getSupportActionBar();` ? How about this line `actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_CUSTOM);` ? – John Nov 15 '15 at 13:57
  • `android.support.v7.app.ActionBar myActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();` and then use `myActionBar` variable to whatever call you want – michal.luszczuk Nov 15 '15 at 13:58
  • Thanks, but why the icon doesn't show on the upper right hand side? – John Nov 15 '15 at 14:06
  • Try to set fixed width,height instead of wrap content, and set color background to the ImageView to debug problem if view is showing of what the problem is – michal.luszczuk Nov 15 '15 at 14:12