简体   繁体   中英

Android ImageButton OnClick giving null pointer exception

Im calling a poup dialog from a menu option that says sign in with: then shows Twitter and Google logos as 2 imagebuttons.

AlertDialog.Builder alertadd = new AlertDialog.Builder(
            SummaryActivity.this);
    LayoutInflater factory = LayoutInflater.from(SummaryActivity.this);
    final View view = factory.inflate(R.layout.social_network_selection, null);
    alertadd.setView(view);

It inflates a view that contains the 2 imagebuttons.

when i add the code

    googleButton2 = (ImageButton)findViewById(R.id.googleButton);
    twitterButton2 = (ImageButton)findViewById(R.id.twitterButton);

All works fine but when i add the

googleButton2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

           Toast.makeText(MyAndroidAppActivity.this,
            "googleButton2 is clicked!", Toast.LENGTH_SHORT).show();

        }

    });

It crashes with a null pointer. Not when i click the button but the second i call the AlertDialog that contains the ImageButtons

How do you add a listener to an imagebutton that is on an AlertDialog inflated View?

LOGCAT

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at android.support.v7.internal.view.SupportMenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(SupportMenuInflater.java:259)
        at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
        at android.support.v7.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)

The issue was in these lines,

googleButton2 = (ImageButton)findViewById(R.id.googleButton);
twitterButton2 = (ImageButton)findViewById(R.id.twitterButton);

findViewById(int) method can not find the id in the context layout therefore it returns null value

Solution

You should have to use the findViewById method to find the view which was inflated by the layout context.

Here is the required solution for your task,

 AlertDialog.Builder alertadd = new AlertDialog.Builder(
                SummaryActivity.this);
    LayoutInflater factory = LayoutInflater.from(SummaryActivity.this);
    final View view = factory.inflate(R.layout.social_network_selection, null);
    alertadd.setView(view);
    googleButton2 = (ImageButton) view.findViewById(R.id.googleButton);
    twitterButton2 = (ImageButton) view.findViewById(R.id.twitterButton);

    googleButton2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

               Toast.makeText(MyAndroidAppActivity.this,
                "googleButton2 is clicked!", Toast.LENGTH_SHORT).show();

            }

        });

Call imagebutton with name of the View view and your onClick use getBaseContext()

 AlertDialog.Builder alertadd = new AlertDialog.Builder(
                SummaryActivity.this);
        LayoutInflater factory = LayoutInflater.from(SummaryActivity.this);
        final View view = factory.inflate(R.layout.social_network_selection, null);
    ImageButton googleButton2 = (ImageButton)view.findViewById(R.id.googleButton);
        ImageButton twitterButton2 = (ImageButton)view.findViewById(R.id.twitterButton);

    googleButton2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

               Toast.makeText(getBaseContext(),
                "googleButton2 is clicked!", Toast.LENGTH_SHORT).show();

            }

        });
        alertadd.setView(view);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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