我正在做我在Android平台上通过加速度计传感器获取数据的项目.我在最后3行代码中有错误代码.确切的错误是findVIEwByID不是用于获取数据片段.那么,我如何在Fragment中使用findvIEwbyID来获取数据加速计传感器?非常感谢答案:)
import androID.app.Activity;import androID.app.Fragment;import androID.content.Context;import androID.harDWare.Sensor;import androID.harDWare.SensorManager;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.TextureVIEw;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.TextVIEw;import androID.harDWare.Sensor;import androID.harDWare.SensorEvent;import androID.harDWare.SensorManager;import androID.harDWare.SensorEventListener;public class GetDataFragment extends Fragment implements SensorEventListener {//definisi variabel-variabel//Sensor accelerometer;private Activity parent;TextVIEw koordinatX;TextVIEw koordinatY;TextVIEw koordinatZ;@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Todo auto-generated method stub return inflater.inflate(R.layout.get_data, container, false);}private SensorManager sensorManager;private Sensor sensor;//TextVIEw koordinatX; @OverrIDepublic voID onCreate(Bundle savedInstanceState) { // Todo auto-generated method stub super.onCreate(savedInstanceState); parent = getActivity(); sensorManager = (SensorManager) parent.getSystemService(Context.SENSOR_SERVICE); sensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); koordinatX = (TextVIEw)findVIEwByID(R.ID.coordinatx); koordinatY = (TextVIEw)findVIEwByID(R.ID.coordinaty); koordinatZ = (TextVIEw)findVIEwByID(R.ID.coordinatz);}
解决方法:
您必须在onCreateVIEw()中膨胀您的视图:
@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { VIEw rootVIEw = inflater.inflate(R.layout.get_data, container, false); koordinatX = (TextVIEw)rootVIEw.findVIEwByID(R.ID.coordinatx); koordinatY = (TextVIEw)rootVIEw.findVIEwByID(R.ID.coordinaty); koordinatZ = (TextVIEw)rootVIEw.findVIEwByID(R.ID.coordinatz); return rootVIEw;}
希望这可以帮助.
总结以上是内存溢出为你收集整理的android – 对于加速计传感器的Get Data Fragment类型,未定义方法findViewById(int)全部内容,希望文章能够帮你解决android – 对于加速计传感器的Get Data Fragment类型,未定义方法findViewById(int)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)