gpt4 book ai didi

java - android获取OnTouchEvent即使应用程序不活动,java空指针

转载 作者:太空宇宙 更新时间:2023-11-04 15:24:13 24 4
gpt4 key购买 nike

我尝试制作一个应用程序,即使它在后台运行,也可以监听触摸事件,在这里我在论坛中找到了解决方案,但我无法运行

How can a service listen for touch gestures/events?

如果我理解正确的话,执行此操作的方法是使用正确的 windowmanager 参数设置一个新 View ,这是我的代码!

主要 Activity :

public class MainActivity extends Activity {


View mView;
HUD mHud;
HUDView mHUDView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mHud = new HUD();
mHUDView = new HUDView(this);

}


@Override
public boolean onTouchEvent(MotionEvent e) {
mHud.onCreate(this);
return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

目前,为了测试,我只想通过第一次触摸来更改 View ,

public class HUD extends Service  {

HUDView mView;

public void onCreate(Context mContext) {
super.onCreate();

mView = new HUDView(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);

params.gravity = Gravity.RIGHT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

}



class HUDView extends ViewGroup {

public HUDView(Context context) {
super(context);
}


@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}

@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("ontouch", "clicked in the HUD View");
//Toast.makeText(getContext(),"onTouchEvent", Toast.LENGTH_LONG).show();
return true;
}
}

我对java不太有信心,我得到的错误是javaNullPointerException错误。我知道发生这种情况是因为一个对象变成了空,但是我如何将对象传递给类和子类。

这里是日志:

11-18 11:56:03.791: E/AndroidRuntime(5095): FATAL EXCEPTION: main
11-18 11:56:03.791: E/AndroidRuntime(5095): java.lang.NullPointerException
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.view.View.<init>(View.java:1810)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.view.ViewGroup.<init>(ViewGroup.java:288)
11-18 11:56:03.791: E/AndroidRuntime(5095): at com.holzi.runinbackground.HUDView.<init>(HUD.java:47)
11-18 11:56:03.791: E/AndroidRuntime(5095): at com.holzi.runinbackground.HUD.onCreate(HUD.java:22)
11-18 11:56:03.791: E/AndroidRuntime(5095): at com.holzi.runinbackground.MainActivity.onTouchEvent(MainActivity.java:32)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
11-18 11:56:03.791: E/AndroidRuntime(5095): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.os.Looper.loop(Looper.java:143)
11-18 11:56:03.791: E/AndroidRuntime(5095): at android.app.ActivityThread.main(ActivityThread.java:4914)
11-18 11:56:03.791: E/AndroidRuntime(5095): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 11:56:03.791: E/AndroidRuntime(5095): at java.lang.reflect.Method.invoke(Method.java:521)
11-18 11:56:03.791: E/AndroidRuntime(5095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)

谢谢!

最佳答案

您将 this 作为上下文传递给 HUDViewthis 是您的 Service,它不在附加之前有一个基本上下文,因此如果调用 getResources() ,则会抛出异常。

您需要等到attachBaseContext被调用,或者使用另一个Context来实例化HUDView而不是服务,也许是Application上下文。

关于java - android获取OnTouchEvent即使应用程序不活动,java空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20046280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com