この「常に一番上のボタン」があり、2.3.7 Ancdroid バージョンのデバイスでタップするとエラーが発生しますが、4.2.2 Android デバイスでタップしてもエラーは発生しません! タッチイベントのコードは次のとおりです。
public class HUD extends Service implements OnClickListener, OnTouchListener, OnLongClickListener {
Button mButton;
@Override
public IBinder
onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//mView = new HUDView(this);
mButton = new Button(this);
mButton.setId(1);
mButton.setText("Button");
mButton.setClickable(true);
mButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.OPAQUE);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mButton, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if(mButton != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mButton);
mButton = null;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) { //onTouchEvent (where is the error)
if(event.getX()<mButton.getWidth() & event.getY()>0)
{
Intent dialogIntent = new Intent(getBaseContext(), screen.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
HUD.this.stopSelf();
}
return false;
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(this,"Click", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
System.exit(1);
return false;
}
}
2.3.7 Android デバイスのタッチ イベントのログ cat は次のとおりです。
07-21 00:01:47.960: E/AndroidRuntime(23542): FATAL EXCEPTION: main
07-21 00:01:47.960: E/AndroidRuntime(23542): java.lang.NullPointerException
07-21 00:01:47.960: E/AndroidRuntime(23542): at com.example.screenshot.HUD.onTouch(HUD.java:75)
07-21 00:01:47.960: E/AndroidRuntime(23542): at android.view.View.dispatchTouchEvent(View.java:3881)
07-21 00:01:47.960: E/AndroidRuntime(23542): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2208)
07-21 00:01:47.960: E/AndroidRuntime(23542): at android.view.ViewRoot.handleMessage(ViewRoot.java:1881)
07-21 00:01:47.960: E/AndroidRuntime(23542): at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 00:01:47.960: E/AndroidRuntime(23542): at android.os.Looper.loop(Looper.java:130)
07-21 00:01:47.960: E/AndroidRuntime(23542): at android.app.ActivityThread.main(ActivityThread.java:3706)
07-21 00:01:47.960: E/AndroidRuntime(23542): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 00:01:47.960: E/AndroidRuntime(23542): at java.lang.reflect.Method.invoke(Method.java:507)
07-21 00:01:47.960: E/AndroidRuntime(23542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-21 00:01:47.960: E/AndroidRuntime(23542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-21 00:01:47.960: E/AndroidRuntime(23542): at dalvik.system.NativeStart.main(Native Method)
誰かが私を助けることができますか?ありがとうございました...