gpt4 book ai didi

Android - fragment 中的 SensorEventListener

转载 作者:行者123 更新时间:2023-11-29 00:28:08 26 4
gpt4 key购买 nike

我正在尝试向我的应用程序中的 fragment 添加一个传感器事件监听器。

下面是我用来定义传感器的代码

Context context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

mLinearLayout = (LinearLayout) inflater.inflate(
R.layout.game, container, false);
this.context = context;

mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
//above is line 49 which is referenced in the logcat
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

//...
return mLinearLayout;
}

这是 logcat 错误

07-27 00:19:50.907: E/AndroidRuntime(20595): FATAL EXCEPTION: main
07-27 00:19:50.907: E/AndroidRuntime(20595): java.lang.NullPointerException
07-27 00:19:50.907: E/AndroidRuntime(20595): at com.Optimus.iwould.Game.onCreateView(Game.java:49)
07-27 00:19:50.907: E/AndroidRuntime(20595): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
07-27 00:19:50.907: E/AndroidRuntime(20595): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)

谢谢

最佳答案

你的上下文是null

this.context = context;

并且您在第 49 行使用上下文

mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);

为空

关于Android - fragment 中的 SensorEventListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17894265/

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