简体   繁体   English

如何通过扩展Button的类调用MainActivity方法

[英]How to call a method of MainActivity by a class that extends Button

I want to call a method showMessage() which is defined in my main activity from a TestButton class that extend Button. 我想从扩展Button的TestButton类中调用在主活动中定义的showMessage()方法。 The code I use below throw a null pointer exception. 我在下面使用的代码引发空指针异常。 I am not sure how to solve this. 我不确定该如何解决。 Can you please help me? 你能帮我么?

MainActivity 主要活动

public class MainActivity extends MultiTouchActivity {

TestButton btn;
@Override
public void onCreate(final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn = (TestButton)findViewById(R.id.button4);
    btn.setOnTouchListener(this);
}
public void showMessage()
{
    Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
}

TestButton class TestButton类

public class TestButton extends Button {

MainActivity m;
public TestButton(final Context context, final AttributeSet attrs)
{
    super(context, attrs);

    // TODO Auto-generated constructor stub
}

@Override
public boolean onTouchEvent(final MotionEvent event)
{
    if (event.getAction() == MotionEvent.ACTION_UP)
    {
        m = new MultitouchtestActivity();
        m.showMessage();
    }
    return super.onTouchEvent(event);
}

} }

EDIT Thank you @ΦXoce 웃 Пepeúpa this solve my issue but I made a little bit changes. 编辑谢谢@ΦXoceППepeúpa,这解决了我的问题,但是我做了一些更改。

btn.setCallback(this); from btn.setCallback();

and

public void setCallback(final ICallback iCallback)

2 things: 2件事:

  1. m = new MultitouchtestActivity(); m = new MultitouchtestActivity(); is not the way ANDROID wants you to create / instantiate Activities.. so you have more chances to break your code than doing something right by calling the constructor directly.. 这不是ANDROID希望您创建/实例化Activity的方式。因此,与直接调用构造函数进行正确操作相比,您有更多的机会破坏代码。
  2. On the other hand trying to call a method from the Activity inside of a button class is forcing you to make circle-dependencies... now your Activity needs a button Object and your button needs an Activity Object..(not a good approach) 另一方面,尝试从按钮类内部的Activity调用方法会迫使您建立圈子依赖关系...现在,您的Activity需要一个按钮Object,而您的按钮需要一个Activity Object ..(不是一个好方法)

How to solve it 如何解决

Implement a callback like doing: 像这样实现回调:

interface ICallback{
    void showMessage();
}

public class MainActivity extends MultiTouchActivity implements ICallback{

TestButton btn;

@Override
public void onCreate(final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn = (TestButton)findViewById(R.id.button4);
    btn.setOnTouchListener(this);
    //register here the callback
    btn.setCallback(this);
}

@Override
public void showMessage()
{
    Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
}

public class TestButton extends Button {

MainActivity m;
ICallback iCallback;
public setCallback(final ICallback iCallback)
{
    this.iCallback = iCallback; 
}
public TestButton(final Context context, final AttributeSet attrs)
{
    super(context, attrs);

    // TODO Auto-generated constructor stub
}

@Override
public boolean onTouchEvent(final MotionEvent event)
{
    if (event.getAction() == MotionEvent.ACTION_UP)
    {
        iCallback.showMessage();
    }
    return super.onTouchEvent(event);
}
}

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

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