本文介绍了该方法findViewById(INT)是未定义的类型获取数据片段的加速度计传感器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做我的项目在android平台acceleometer传感器获取数据。我有在3最后一行codeS错误codeS。正是误差是findViewById没有为获取数据片段。所以,我怎么能在片段使用findviewbyId获取数据加速计传感器非常感谢的答案:?)

 进口android.app.Activity;
进口android.app.Fragment;
进口android.content.Context;
进口android.hardware.Sensor;
进口android.hardware.SensorManager;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.TextureView;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorManager;
进口android.hardware.SensorEventListener;
公共类GetDataFragment扩展片段实现SensorEventListener {// definisi variabel,variabel
// Sensor重力感应;
私人活动父;
TextView的koordinatX;
TextView的koordinatY;
TextView的koordinatZ;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    // TODO自动生成方法存根
    返回inflater.inflate(R.layout.get_data,集装箱,FALSE);
}私人的SensorManager的SensorManager;
私人传感器传感器;
// TextView的koordinatX;
    @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);     父= getActivity();
    的SensorManager =(的SensorManager)parent.getSystemService(Context.SENSOR_SERVICE);
    传感器= sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER)获得(0);
    koordinatX =(的TextView)findViewById(R.id.coordinatx);
    koordinatY =(的TextView)findViewById(R.id.coordinaty);
    koordinatZ =(的TextView)findViewById(R.id.coordinatz);
}


解决方案

您必须抬高在 onCreateView你的查看()

  @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    查看rootView = inflater.inflate(R.layout.get_data,集装箱,FALSE);
    koordinatX =(TextView中)rootView.findViewById(R.id.coordinatx);
    koordinatY =(TextView中)rootView.findViewById(R.id.coordinaty);
    koordinatZ =(TextView中)rootView.findViewById(R.id.coordinatz);
    返回rootView;
}

希望这有助于。

I'm doing my project for getting data by acceleometer sensor in android platform. I'm having error codes in the 3 last line codes. Exactly the error is the findViewById is not for get data fragment. so, how I can use findviewbyId in Fragment for getting data accelerometer sensor?many thanks for the answer :)

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;

@Override
public 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;


    @Override
public 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);


}
解决方案

You must inflate your Views in onCreateView():

@Override
public 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;
}

Hope this helps.

这篇关于该方法findViewById(INT)是未定义的类型获取数据片段的加速度计传感器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 19:58