片段中的TextView.setText()崩溃应用程序

时间:2018-04-25 09:23:20

标签: android crash fragment sensor

我正在尝试更改传感器更改的TextView内容,但在调用TextView.setText("foo");应用程序崩溃后,好像TextView变量为空。我在TextView中设置了onCreateView变量。这是代码:

public class fragment1 extends Fragment implements SensorEventListener
{
    private SensorManager mSrMgr = null;
    private Sensor mLight;
    private TextView page1;
    protected View mView;


    public fragment1()
    {
        // Required empty public constructor
    }

    public void onCreate(Bundle savedInstance)
    {
        super.onCreate(savedInstance);
        mSrMgr = (SensorManager) getActivity().getSystemService(SENSOR_SERVICE);
        mLight = mSrMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        page1 = container.findViewById(R.id.page1);
        View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
        this.mView = view;
        return inflater.inflate(R.layout.fragment_fragment1, container, false);
    }

    public void onPause()
    {
        super.onPause();
        this.unregister();
    }

    public void onResume()
    {
        super.onResume();
        this.registerSensorListiner();
    }

    private void registerSensorListiner()
    {
        mSrMgr.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
    }

    private void unregister()
    {
        mSrMgr.unregisterListener(this);
    }


    @Override
    public void onSensorChanged(SensorEvent event)
    {
        Toast.makeText(getContext(),String.valueOf(event.values[0]),Toast.LENGTH_SHORT).show();
        page1.setText("elo"); //here is the crashing part
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {

    }
}

6 个答案:

答案 0 :(得分:1)

为什么多次 R.layout.

  View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
        this.mView = view;
        return inflater.inflate(R.layout.fragment_fragment1, container, false);

应该是一次性的。首先修改一下。

注意

您将获得NullPointerException

  

当应用程序在某个情况下尝试使用null时抛出   对象是必需的。

您应该传递 View's object

page1 = view.findViewById(R.id.page1);

<强>最后

 View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
 page1 = view.findViewById(R.id.page1);
 this.mView = view;
 return view;

答案 1 :(得分:1)

首先,夸大布局

View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
this.mView = view;
page1 = view.findViewById(R.id.page1); 
return view, false);

使用view对象来调用findViewById方法

答案 2 :(得分:0)

使用此

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{            
   View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
   this.mView = view;
   page1 = view.findViewById(R.id.page1);
   return view;
}

而不是这个

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        page1 = container.findViewById(R.id.page1);
        View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
        this.mView = view;
        return inflater.inflate(R.layout.fragment_fragment1, container, false);
    }

答案 3 :(得分:0)

public class fragment1 extends Fragment implements SensorEventListener {
    private SensorManager mSrMgr = null;
    private Sensor mLight;
    private TextView page1;
    protected View mView;


    public fragment1() {
        // Required empty public constructor
    }

    public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        mSrMgr = (SensorManager) getActivity().getSystemService(SENSOR_SERVICE);
        mLight = mSrMgr.getDefaultSensor(Sensor.TYPE_LIGHT);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_fragment1, container, false);
        page1 = mView.findViewById(R.id.page1);
        return mView;
    }

    public void onPause() {
        super.onPause();
        this.unregister();
    }

    public void onResume() {
        super.onResume();
        this.registerSensorListiner();
    }

    private void registerSensorListiner() {
        mSrMgr.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
    }

    private void unregister() {
        mSrMgr.unregisterListener(this);
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        Toast.makeText(getContext(), String.valueOf(event.values[0]), Toast.LENGTH_SHORT).show();
        page1.setText("elo"); //here is the crashing part
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}

答案 4 :(得分:0)

findViewById代码放在onActivityCreated中,如下所示:

 @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        page1 = getActivity().findViewById(R.id.page1);
    }

答案 5 :(得分:0)

有时在片段中创建布局时,您将粘贴并粘贴具有重复ID的xml代码。然后,当您调用粘贴的资源的ID时,它将尝试调用第一个定义的资源,该资源在运行时可能为null。仔细检查是否仅在一个片段的Xml中找到page1 id。如果无法解决“ findviewbyid”。希望对您有所帮助,您的代码中几乎没有细节(错误消息)