我有这个“总是在顶部按钮”并且当我在我的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设备触摸事件的日志猫:
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)
有人能帮帮我吗?谢谢......