Java中,使用一个接口作为回调 [英] Java, using an interface as a callback

查看:296
本文介绍了Java中,使用一个接口作为回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发针对Android与发射一样的onUpdate(当屏幕被触摸)回调的possibilites一个简单的触摸处理程序,而无需设置线程。我的问题是,我的Java知识是相当有限的,我不能这样做,因为我知道很少如何使用接口。我是pretty确保我的问题可能是一个简单的拼写错误或东西,但我得到一个NullPointerException异常,当我执行从触摸处理器的方法(处理的触摸信息),这样我可以做什么,我需要在主要活动类。

这是主类code(从不相干的东西切):

  //包进口公共类测试扩展活动实现TouchHelper {    StringBuilder的建设者=新的StringBuilder();
    TextView中的TextView;
    TouchReader touchReader;
    清单< TouchTable> touchTablesArray;
    TouchTable touchTable;
    公共静态最后的字符串标记=TouchTest;        @覆盖
        公共无效的onCreate(捆绑savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            TextView中=新的TextView(本);
            Log.d(标签,TextView的初始化+的TextView);
            textView.setText(触摸和阻力(支持多个手指)!);
            touchReader =新TouchReader(的TextView);
            Log.d(标签,touchReader初始化);
            touchTablesArray = touchReader.getTouchTables();
            的setContentView(TextView的);
        }        @覆盖
        公共无效onTouchUpdate(INT pointerId)
        {
            Log.d(标签,onTouchUpdate称为);
            touchTable = touchTablesArray.get(pointerId);
            Log.d(标签,touchTable获得成功);
            // StringBuilder的上写
        }
    }

这是处理程序本身的code:

  //包进口公共类TouchReader实现OnTouchListener
{
    公众最终静态字符串变量=TouchReader;
    清单< TouchTable> touchTables;
    TouchHelper帮手;
    TouchTable touchTable =新TouchTable();    公共TouchReader(查看视图)
    {
        view.setOnTouchListener(本);
        touchTables =新的ArrayList< TouchTable>(10);
        Log.d(标签,TouchReader初始化);
    }    公共布尔onTouch(视图V,MotionEvent事件)
    {
        同步(本)
        {
            //所有常见code处理实际处理,具有开关和这样的
            touchTables.add(pointerId,touchTable); //显然pointerId前面定义
            Log.d(标签,来更新);
            helper.onTouchUpdate(pointerId); //例外是在这里
            Log.d(标签,更新名为);
        }
        返回true;
    }
    公开名单< TouchTable> getTouchTables()
    {
        同步(本)
        {
            返回touchTables;
        }
    }
}

正如你所看到的错误很可能是由于我无法正确地使用一个接口,但所有的官方文档搞糊涂了,甚至更多。

最后,该界面的微小code:

  //包公共接口TouchHelper
{
    公共无效onTouchUpdate(INT pointerId);
}

我希望这个问题不是太noobish它张贴在这里:)

编辑:感谢所有的帮助,最后我也跟着Bughi的解决方案


解决方案

TouchHelper帮手; 为空,它需要的接口的实例能够调用的方法它-in您的情况下实现你的界面 - 主要活动类

请了一套方法让听者

 公共无效setOnTouchListener(TouchHelper帮手)
{
    this.helper =帮手;
}

然后从创建调用它

 公共类测试扩展活动实现TouchHelper {
    ...
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        ...
        touchReader =新TouchReader(的TextView);
        touchReader.setOnTouchListener(本);
        ...
    }
}

还添加了一个空检查你的触摸方式:

 公共布尔onTouch(视图V,MotionEvent事件)
{
    同步(本)
    {
        //所有常见code处理实际处理,具有开关和这样的
        touchTables.add(pointerId,touchTable); //显然pointerId前面定义
        Log.d(标签,来更新);
        如果(帮手!= NULL)
            helper.onTouchUpdate(pointerId); //例外是在这里
        Log.d(标签,更新名为);
    }
    返回true;
}

I have been developing a simple touch handler for Android with the possibilites of firing callbacks like onUpdate (when the screen is touched) without having to setup threads. My problem is that my knowledge of Java is fairly limited and i can't do it because i know very little of how to use interfaces. I'm pretty sure that my problem may be a simple typo or something, but i get a NullPointerException when i execute the method from the touch handler (which processed the touch information) so that i can do what i need in the main activity class.

This is the main class code (cut from the irrelevant stuff):

//package and imports

public class Test extends Activity implements TouchHelper {

    StringBuilder builder = new StringBuilder();
    TextView textView;
    TouchReader touchReader;
    List<TouchTable> touchTablesArray;
    TouchTable touchTable;
    public static final String Tag = "TouchTest";

        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            textView = new TextView(this);
            Log.d(Tag, "TextView initialized " + textView);
            textView.setText("Touch and drag (multiple fingers supported)!");
            touchReader = new TouchReader(textView);
            Log.d(Tag, "touchReader initialized");
            touchTablesArray = touchReader.getTouchTables();
            setContentView(textView);
        }

        @Override
        public void onTouchUpdate(int pointerId)
        {
            Log.d(Tag, "onTouchUpdate called");
            touchTable = touchTablesArray.get(pointerId);
            Log.d(Tag, "touchTable get successful");
            //writing on stringbuilder
        }
    }

This is the code of the handler itself:

//package and imports

public class TouchReader implements OnTouchListener
{
    public final static String Tag = "TouchReader";
    List<TouchTable> touchTables;
    TouchHelper helper;
    TouchTable touchTable = new TouchTable();

    public TouchReader(View view)
    {
        view.setOnTouchListener(this);
        touchTables = new ArrayList<TouchTable>(10);
        Log.d(Tag, "TouchReader initialized");
    }

    public boolean onTouch(View v, MotionEvent event)
    {
        synchronized(this)
        {
            //all the common code handling the actual handling, with switches and such
            touchTables.add(pointerId, touchTable); //obviously the pointerId is defined earlier
            Log.d(Tag, "Values updated");
            helper.onTouchUpdate(pointerId); //the exception is here
            Log.d(Tag, "Update called");
        }
        return true;
    }
    public List<TouchTable> getTouchTables()
    {
        synchronized(this)
        {
            return touchTables;
        }
    }
}

As you can see the error is most likely due to my inability to correctly use an interface, and yet all the official docs confused me even more.

Finally, the tiny code of the interface:

//package

public interface TouchHelper 
{
    public void onTouchUpdate(int pointerId);
}

I hope this question isn't too noobish to post it here :)

EDIT: Thanks to all for the help, in the end i followed Bughi's solution.

解决方案

Your TouchHelper helper; is null, it needs a instance of the interface to be able to call methods on it -in your case the main activity class that implements your interface-

Make a set method for the listener

public void setOnTouchListener(TouchHelper helper)
{
    this.helper = helper;
}

Then call it from on create:

public class Test extends Activity implements TouchHelper {
    ...
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        ...
        touchReader = new TouchReader(textView);
        touchReader.setOnTouchListener(this);
        ...
    }
}

Also add a null check to your on touch method:

public boolean onTouch(View v, MotionEvent event)
{
    synchronized(this)
    {
        //all the common code handling the actual handling, with switches and such
        touchTables.add(pointerId, touchTable); //obviously the pointerId is defined earlier
        Log.d(Tag, "Values updated");
        if (helper != null)
            helper.onTouchUpdate(pointerId); //the exception is here
        Log.d(Tag, "Update called");
    }
    return true;
}

这篇关于Java中,使用一个接口作为回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆