按钮在Onclick上没有响应?

时间:2014-05-31 07:55:43

标签: android facebook onclicklistener

我创建了一个按钮,通过FB应用程序或浏览器连接到我的FB页面。只要单击该按钮,就没有响应。我不知道我在哪里犯了错误

所以请指导我......

public class About extends MainActivity implements android.view.View.OnClickListener
{
    Button fb;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.about, null, false);
        mDrawer.addView(contentView, 0);
        fb = (Button) contentView.findViewById(R.id.fb);
        fb.setOnClickListener(this);
    }

    public static Intent getOpenFacebookIntent(Context context)
    {
        try
        {
            context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
            return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/1493553"));
        }
        catch (Exception e)
        {
            return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/azaarses"));
        }
    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        if (v == fb)
        {
            getOpenFacebookIntent(getApplicationContext());
        }
    }

}

3 个答案:

答案 0 :(得分:3)

试试这个

方法1:

@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub
    if (v.getId() == R.id.fb)
    {
        getOpenFacebookIntent(getApplicationContext());
    }
}

方法2:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.fb:
            getOpenFacebookIntent(getApplicationContext());
        break;

    case R.id.othercases:
            //other cases and their functions.
        break;

    default:
        break;
    }
}

确保您为onClickListener()

定义了Button
fb = (Button) contentView.findViewById(R.id.fb);
fb.setOnClickListener(this); // defining OnClickListener() for specific `Button`..

这可能对你有帮助..

答案 1 :(得分:1)

您正在使用赋值运算符比较两个对象,它始终为false ..而不是比较两个对象尝试比较视图的ID。

if (v.getId() == R.id.fb)

答案 2 :(得分:0)

最常用的方法是使用按钮属性:

android:onClick="yourMethodInActivity"

首先确保在yourMethodInActivity()的开头用Log.e输入方法。

可以打印堆栈跟踪吗?