0

I wanted to make an icon be clickable but the app always crashed. I follow here but no success. It is a must to have the first step? Once the icon is clicked, it will go to AlertRadioDialog function, which mean a dialog radio should be pop out. How to achieve this?

      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Bundle bundle = this.getArguments();
            fk = bundle.getLong("ab");
            setHasOptionsMenu(true);
            getActivity().getActionBar().setHomeButtonEnabled(true);
            View claims = inflater.inflate(R.layout.claims, container, false);
            android.support.v7.app.ActionBar myActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
            ImageView imageView = new ImageView(myActionBar.getThemedContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageView.setImageResource(R.mipmap.create);
            android.support.v7.app.ActionBar.LayoutParams layoutParams = new android.support.v7.app.ActionBar.LayoutParams(
                    android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT,
                    android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                    | Gravity.CENTER_VERTICAL);
            layoutParams.rightMargin = 40;
            imageView.setLayoutParams(layoutParams);
            myActionBar.setCustomView(imageView);
            return claims;
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.my_menu, menu);
            super.onCreateOptionsMenu(menu, inflater);
            //Do what you want with menu - listeners etc.
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    AlertDialogRadio();
                    return true;
            }
            return (super.onOptionsItemSelected(item));
        }

     public void AlertDialogRadio() {
            final CharSequence[] ClaimsModel = {"A", "B"};

            AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
            alt_bld.setTitle("Select a Class");
            alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
                    .OnClickListener() {

                public void onClick(DialogInterface dialog, int item) {
                    if (item == 0) {
                        Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
                        startActivityForResult(intent, 0);
                    } else if (item == 1) {
                        Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
        }
                       dialog.dismiss();

            }
        });
        AlertDialog alert = alt_bld.create();
        alert.show();
    }
}

Error

11-15 16:42:25.459  20826-20826/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.project, PID: 20826
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
            at com.example.project.project.Claims1.onCreateView(Claims1.java:34)
            at android.app.Fragment.performCreateView(Fragment.java:2220)
John
  • 684
  • 11
  • 35
  • `getActionBar()` returns null. Take a look [here](http://stackoverflow.com/a/10031400/1584654) – GVillani82 Nov 15 '15 at 17:04
  • @Joseph82 `getActivity().getActionBar().setHomeButtonEnabled(true);` – John Nov 15 '15 at 17:06
  • @Joseph82 I use `myActionBar.setHomeButtonEnabled(true);` and add after `android.support.v7.app.ActionBar myActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();`. The issue solved. But when I click the icon, nothing pop out. It is because of the `case android.R.id.home:` ? – John Nov 15 '15 at 17:13
  • According to the link I provided, you should use this code `getWindow().requestFeature(Window.FEATURE_ACTION_BAR);` before calling `getActionBar()` – GVillani82 Nov 15 '15 at 17:14
  • Would it solved after added the code you provided? Between, getWindow() cannot be resolved :) – John Nov 15 '15 at 17:19

1 Answers1

0

Make sure that you have a theme for that activity in the manifest that supports actionbar. If you use AppCompatActivity the try using getSupportActionBar()

z3n105
  • 1,935
  • 1
  • 15
  • 14
  • ya, I did. But when I click the icon, nothing display. It seems like not going to `AlertDialogRadio()`. Is it because I using the wrong id? `(android.R.id.home:)` – John Nov 15 '15 at 17:32
  • We need to see what this method does for you AlertDialogRadio(); – z3n105 Nov 15 '15 at 17:35
  • Done. So it is a Alert Dialog Window with radio buttons. I just don't understand where the `(android.R.id.home)` came from. – John Nov 15 '15 at 17:39
  • where the (android.R.id.home) came from? I saw my tutorial using this. Is it an icon id or what? @@ – John Nov 15 '15 at 17:50
  • It is the id of your icon. Remove the android, just write R.id.YourIconID. – John Nov 15 '15 at 17:59