Java中如何实现接口回调,Java中,使用一个接口作为回调

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 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 touchTables;

TouchHelper helper;

TouchTable touchTable = new TouchTable();

public TouchReader(View view)

{

view.setOnTouchListener(this);

touchTables = new ArrayList(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 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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值