代码之家  ›  专栏  ›  技术社区  ›  hdw3

文本视图。片段崩溃应用程序中的setText()

  •  -1
  • hdw3  · 技术社区  · 6 年前

    我正在努力改变 TextView 传感器上的内容更改,但在调用后 TextView.setText("foo"); 应用程序崩溃,就像 文本框 变量为null。我设置了 文本框 变量输入 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 回复  |  直到 6 年前
        1
  •  2
  •   AskNilesh    6 年前

    首先,为布局充气

    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
  •  1
  •   AskNilesh    6 年前

    使用此

    @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
  •  1
  •   IntelliJ Amiya    6 年前

    为什么多次 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

    当应用程序在 对象是必需的。

    你应该通过 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;
    
        4
  •  0
  •   Deep    6 年前
    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) {
    
        }
    }
    
        5
  •  0
  •   Hitesh Sarsava    6 年前

    把你的 findViewById 代码输入 onActivityCreated 如下所示:

     @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            page1 = getActivity().findViewById(R.id.page1);
        }
    
        6
  •  0
  •   NgoniDee    5 年前

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