简体   繁体   English

Android ImageButton OnClick 给出空指针异常

[英]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.我从一个菜单选项中调用一个弹出对话框,上面写着登录:然后将 Twitter 和 Google 徽标显示为 2 个图像按钮。

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.它膨胀包含 2 个图像按钮的视图。

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不是当我点击按钮时,而是第二次我调用包含 ImageButtons 的 AlertDialog

How do you add a listener to an imagebutton that is on an AlertDialog inflated View?如何将侦听器添加到 AlertDialog 膨胀视图上的图像按钮?

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 findViewById(int) 方法在上下文布局中找不到 id 因此它返回

Solution解决方案

You should have to use the findViewById method to find the view which was inflated by the layout context.您应该必须使用 findViewById 方法来查找由布局上下文膨胀的视图。

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()使用 View 视图的名称调用 imagebutton,您的 onClick 使用 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);

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

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