java.lang.IllegalStateException:在onCreate()之前,活动无法使用系统服务,但是我已经做了

时间:2019-05-21 22:27:35

标签: java android sensor oncreate sensormanager

我试图在ExpandableList内的TextView中显示设备的传感器列表,但是会发生此错误:

  

E / Android运行时:致命异常:主要       流程:com.example.tpcubv4,PID:8045       java.lang.IllegalStateException:在onCreate()之前,Activities无法使用系统服务           在android.app.Activity.getSystemService(Activity.java:5998)           在com.example.tpcubv4.control.DeviceSensors.sensorsList(DeviceSensors.java:21)           在com.example.tpcubv4.MainActivity $ 1.onChildClick(MainActivity.java:45)           在android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:714)           在android.widget.ExpandableListView.performItemClick(ExpandableListView.java:653)           在android.widget.AbsListView $ PerformClick.run(AbsListView.java:4094)           在android.widget.AbsListView $ 10.run(AbsListView.java:6583)           在android.os.Handler.handleCallback(Handler.java:789)           在android.os.Handler.dispatchMessage(Handler.java:98)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6944)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

但是我正在使用onCreate()中的方法访问系统:

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buildList();

    ExpandableListView expandableListView = findViewById(R.id.elv_expandable_list_view);
    expandableListView.setAdapter(new ExpandableAdapter(MainActivity.this, listGroup, listData));

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            if (listCurrentValue.equals(3)) {
                DeviceSensors deviceSensors = new DeviceSensors();
                deviceSensors.sensorsList();
                Toast.makeText(MainActivity.this, "Sensors List", Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    });

    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(MainActivity.this, "List opened", Toast.LENGTH_SHORT).show();
        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(MainActivity.this, "List closed", Toast.LENGTH_SHORT).show();
        }
    });

    expandableListView.setGroupIndicator(getResources().getDrawable(R.drawable.icon_group));

}

此方法在另一个类中:

public void sensorsList() {
    **sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);**
    List<Sensor> deviceSensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
    TextView sensorsList = findViewById(R.id.tv_item);
    String value = new String("");
    sensorsList.setText(value);
    StringBuffer stringBuffer = new StringBuffer();
    for (int i = 0; i < deviceSensors.size(); i++) {
        sensorsList.setText(sensorsList.getText() + deviceSensors.get(i).getName() + "\n");
        stringBuffer.append(deviceSensors.get(i).getName() + "\n");
    }
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    if (sensor != null)
        Toast.makeText(getApplicationContext(), "Detected the following sensors:\n" + stringBuffer, Toast.LENGTH_LONG);
    else
        Toast.makeText(getApplicationContext(), "There are no sensors", Toast.LENGTH_SHORT);
}

请注意,我的方法 onChildClick 位于 onCreate 方法

0 个答案:

没有答案