简体   繁体   English

无法在操作栏的右上方添加图标

[英]Failed to add icon in the upper right hand side of action bar

I'm trying to add an button icon in the upper right hand side of action bar and I follow this tutorial 我正在尝试在操作栏的右上角添加按钮图标,并且按照本教程进行操作

Claims1.java 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 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 ? 我是否需要在claims1.xml添加任何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) (Claims1.java:26)

 actionBar.setDisplayOptions(actionBar.getDisplayOptions()
                | ActionBar.DISPLAY_SHOW_CUSTOM);

You are using wrong getActionBar method. 您使用了错误的getActionBar方法。

You are using AppCompatActivity so action bar is from supported library. 您正在使用AppCompatActivity因此操作栏来自受支持的库。 You should use ((AppCompatActivity) getActivity()).getSupportActionBar(); 您应该使用((AppCompatActivity) getActivity()).getSupportActionBar();

If you not make cast getActionBar is asking about android.app.ActionBar but using AppCompatActivity you need to ask about android.support.v7.app.ActionBar 如果不进行getActionBar会询问android.app.ActionBar但要使用AppCompatActivity询问android.support.v7.app.ActionBar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM