Android ImageButton OnClick给出空指针异常

Android ImageButton OnClick给出空指针异常,android,Android,我从一个菜单选项中调用了一个poup对话框,上面写着登录:然后将Twitter和Google徽标显示为两个图像按钮 AlertDialog.Builder alertadd = new AlertDialog.Builder( SummaryActivity.this); LayoutInflater factory = LayoutInflater.from(SummaryActivity.this); final View view = factory

我从一个菜单选项中调用了一个poup对话框,上面写着登录:然后将Twitter和Google徽标显示为两个图像按钮

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);
它会对包含2个ImageButton的视图进行充气

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)findViewById(R.id.googleButton);
    twitterButton2 = (ImageButton)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();

        }

    });
它因空指针而崩溃。不是在单击按钮时,而是在第二次调用包含ImageButtons的AlertDialog时

如何将侦听器添加到AlertDialog视图上的imagebutton

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)

使用视图名和onClick use getBaseContext()调用imagebutton


问题就是这样,

googleButton2 = (ImageButton)findViewById(R.id.googleButton);
twitterButton2 = (ImageButton)findViewById(R.id.twitterButton);
findViewById(int)方法无法在上下文布局中找到id,因此它返回null解决方案您应该使用findViewById方法来查找由布局上下文膨胀的视图 这是您的任务所需的解决方案,

 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();

            }

        });

请张贴日志,以便我们检查。请看此链接: