简体   繁体   English

Android:跨类启动监听器

[英]Android: start listener across classes

Hey I am currently working myself my way into custom views on Android but I now run into a problem setting a listener. 嘿,我目前正在以自己的方式进入Android上的自定义视图,但是现在遇到了设置监听器的问题。 If I call the listener from within the custom view class, everything works, but I want to set the listener from my MainActivity which is where I am running into problems right now. 如果我从自定义视图类中调用侦听器,则一切正常,但是我想从MainActivity设置侦听器,这是我现在遇到问题的地方。 Every time, I call the Listener from my MainActivity I just get a nullpointer, no matter what I pass to the method. 每当我从MainActivity调用Listener时,无论传递给该方法什么,我都只会得到一个nullpointer。 Here the code of the custom class 这是自定义类的代码

public class RoundKnobButton extends RelativeLayout implements OnGestureListener {

public interface RoundKnobButtonListener {
    public void onStateChange(boolean newstate) ;
    public void onRotate(int percentage);
}


public RoundKnobButtonListener m_listener;

public void SetListener(RoundKnobButtonListener l) {
    m_listener = l;
} 

and if I call the Listener from within this class like 如果我从此类中调用Listener

     SetListener(new RoundKnobButtonListener() {
            public void onStateChange(boolean newstate) {

            }

            public void onRotate(final int percentage) {
            System.out.println(percentage); 
     }
                }); 

It returns the value perfectly. 它完美地返回值。 Now, when I want to do something like 现在,当我想做类似的事情

rv=(RoundKnobButton) findViewById(R.id.jogView);
rv.SetListener(new RoundKnobButton.RoundKnobButtonListener() {
public void onStateChange(boolean newstate) {
}

public void onRotate(final int percentage) {
    System.out.println(percentage); 
        }
    }); 

in my MainActivity, I get 在我的MainActivity中,我得到

02-10 14:31:45.354: W/dalvikvm(19006): threadid=1: thread exiting with uncaught exception (group=0x418259a8)
02-10 14:31:45.360: E/AndroidRuntime(19006): FATAL EXCEPTION: main
02-10 14:31:45.360: E/AndroidRuntime(19006): java.lang.NullPointerException
02-10 14:31:45.360: E/AndroidRuntime(19006):    at  .buildUI( .java:191)
02-10 14:31:45.360: E/AndroidRuntime(19006):    at  .access$6( .java:125)
02-10 14:31:45.360: E/AndroidRuntime(19006):    at  .onPostExecute( .java:334)

Where line 191 refers to rv.SetListener(new RoundKnobButton.RoundKnobButtonListener() 第191行指的是rv.SetListener(new RoundKnobButton.RoundKnobButtonListener()

I already tried passing all kinds of parameters with all kinds of casts, but nothing seems to help here. 我已经尝试过通过各种类型的转换传递各种参数,但是似乎没有什么帮助。

Thanks in advance, Alex 预先感谢Alex

Thanks to KevDev, I found the problem. 感谢KevDev,我找到了问题所在。 I called 我打了电话

super(context);

but I shold've called super(context,attrs); 但我称呼为super(context,attrs); in my constructer as it is public RoundKnobButton(Context context, AttributeSet attrs) otherwise it of course can't return the ID 在我的构造函数中,因为它是public RoundKnobButton(Context context, AttributeSet attrs)否则它当然不能返回ID

Thanks 谢谢

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

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