私は、スレッドを設定することなく、onUpdate(画面がタッチされたとき)のようなコールバックを起動する可能性のあるAndroid用のシンプルなタッチハンドラーを開発してきました。私の問題は、Javaの知識がかなり限られており、インターフェイスの使用方法をほとんど知らないため、それができないことです。私の問題は単純なタイプミスか何かである可能性が高いと確信していますが、メインアクティビティクラスで必要なことを実行できるように、タッチハンドラー(タッチ情報を処理した)からメソッドを実行するとNullPointerExceptionが発生します。
これはメインのクラスコードです(無関係なものから切り取ったものです):
//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
}
}
これは、ハンドラー自体のコードです。
//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;
}
}
}
ご覧のとおり、エラーはインターフェイスを正しく使用できないことが原因である可能性が高いですが、すべての公式ドキュメントでさらに混乱しています。
最後に、インターフェースの小さなコード:
//package
public interface TouchHelper
{
public void onTouchUpdate(int pointerId);
}
私はこの質問がここに投稿するのにあまりにも気難しいものではないことを願っています:)
編集:助けてくれたすべての人に感謝します、結局私はブギの解決策に従いました。