Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有getSupportFragmentManager函数的nullPointerException_Android_Null - Fatal编程技术网

Android 带有getSupportFragmentManager函数的nullPointerException

Android 带有getSupportFragmentManager函数的nullPointerException,android,null,Android,Null,我需要你的帮助。我正在尝试从智能手机的传感器读取数据。这就是为什么我试着写一些代码。过了一会儿,我在网上找到了一个密码。但它不工作,我不知道为什么。在该代码中,有两个页面,一个用于选择传感器,另一个用于获取数据 我在这里复制代码。最后我会告诉你问题出在哪里 这是主要的活动代码 package com.example.sensorlistactivity; import android.os.Bundle; import android.support.v4.app.FragmentA

我需要你的帮助。我正在尝试从智能手机的传感器读取数据。这就是为什么我试着写一些代码。过了一会儿,我在网上找到了一个密码。但它不工作,我不知道为什么。在该代码中,有两个页面,一个用于选择传感器,另一个用于获取数据

我在这里复制代码。最后我会告诉你问题出在哪里

这是主要的活动代码

  package com.example.sensorlistactivity;
  import android.os.Bundle;
  import android.support.v4.app.FragmentActivity;


  public class SensorListActivity extends FragmentActivity {

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

    SensorSelectorFragment sensorSelect = (SensorSelectorFragment) getSupportFragmentManager()
            .findFragmentById(R.id.frag_sensor_select);

    SensorDisplayFragment sensorDisplay = (SensorDisplayFragment) getSupportFragmentManager()
            .findFragmentById(R.id.frag_sensor_view);

    sensorSelect.setSensorDisplay(sensorDisplay);
}

}
这是传感器选择器代码

    package com.example.sensorlistactivity;

    import java.util.List;
    import android.app.Activity;
    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.app.ListFragment;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;

    public class SensorSelectorFragment extends ListFragment {

        private static final String TAG = "SensorSelectorFragment";
        private SensorDisplayFragment sensorDisplay;

        public void setSensorDisplay(SensorDisplayFragment sensorDisplay) {
            this.sensorDisplay = sensorDisplay;
            SensorManager sensorManager = (SensorManager) getActivity()
                    .getSystemService(Activity.SENSOR_SERVICE);
            List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
            this.setListAdapter(new SensorListAdapter(getActivity()
                    .getApplicationContext(), android.R.layout.simple_list_item_1,
                    sensors));
        }

        private void showSensorFragment(Sensor sensor) {
            sensorDisplay.displaySensor(sensor);
            FragmentTransaction ft = getActivity().getSupportFragmentManager()
                    .beginTransaction();
            ft.hide(this);
            ft.show(sensorDisplay);
            ft.addToBackStack("Showing sensor: " + sensor.getName());
            ft.commit();
        }

        private class SensorListAdapter extends ArrayAdapter<Sensor> {

            public SensorListAdapter(Context context, int textViewResourceId,
                    List<Sensor> sensors) {

                super(context, textViewResourceId, sensors);
            }

            @Override
            public View getView(final int position, View convertView,
                    ViewGroup parent) {

                final Sensor selectedSensor = getItem(position);
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(
                            android.R.layout.simple_list_item_1, null);
                }
                ((TextView) convertView).setText(selectedSensor.getName());

                convertView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (BuildConfig.DEBUG) {
                            Log.d(TAG,
                                    "display sensor! " + selectedSensor.getName());
                        }
                        showSensorFragment(selectedSensor);
                    }
                });
                return convertView;
            }
        }
    }
问题是:当我运行这段代码时,有一个nullPointerExpection。 在主活动中,“传感器选择”和“传感器显示”为空。换句话说,这部分不起作用

    SensorSelectorFragment sensorSelect = (SensorSelectorFragment)     getSupportFragmentManager()
            .findFragmentById(R.id.frag_sensor_select);

    SensorDisplayFragment sensorDisplay = (SensorDisplayFragment) getSupportFragmentManager()
            .findFragmentById(R.id.frag_sensor_view);
为什么会这样? 我还使用了两个layout.xml文件。其中一个是由主代码调用的sensor_main,另一个是sensor_view

我把这些代码也放在这里,这些代码可能也有问题,因为我不明白为什么可能有两个布局xml文件

SENSOR_MAIN.XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical"
       >
        <fragment
            android:id="@+id/frag_sensor_select"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            class="root.gast.playground.sensor.SensorSelectorFragment" />

        <fragment
            android:id="@+id/frag_sensor_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            class="root.gast.playground.sensor.SensorDisplayFragment" />
    </LinearLayout>

SENSOR_VIEW.XML

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <RadioGroup android:id="@+id/sensorRateSelector" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true" >

                <RadioButton android:id="@+id/delayFastest"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="SENSOR_DELAY_FASTEST"
                    android:checked="false"/>

                <RadioButton android:id="@+id/delayGame"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="SENSOR_DELAY_GAME"
                    android:checked="false"/>

                <RadioButton android:id="@+id/delayNormal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="SENSOR_DELAY_NORMAL"
                    android:checked="true"/>

                <RadioButton android:id="@+id/delayUi"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="SENSOR_DELAY_UI"
                    android:checked="false"/>
            </RadioGroup>

            <View android:id="@+id/seperator"
                style="@style/line_separator"
                android:layout_below="@id/sensorRateSelector" />

            <TextView android:id="@+id/nameLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/seperator"
                android:layout_alignParentLeft="true"
                android:text="Name:"
                android:layout_marginRight="5dip" />

            <TextView android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/nameLabel"
                android:layout_alignTop="@id/nameLabel"
                android:layout_alignBottom="@id/nameLabel" />

            <TextView android:id="@+id/typeLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/nameLabel"
                android:layout_below="@id/nameLabel"
                android:text="Type:"
                android:layout_marginRight="5dip" />

            <TextView android:id="@+id/type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/typeLabel"
                android:layout_alignTop="@id/typeLabel"
                android:layout_alignBottom="@id/typeLabel"/>

            <TextView android:id="@+id/maxRangeLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/nameLabel"
                android:layout_below="@id/typeLabel"
                android:text="Max Range:"
                android:layout_marginRight="5dip" />

            <TextView android:id="@+id/maxRange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/maxRangeLabel"
                android:layout_alignTop="@id/maxRangeLabel"
                android:layout_alignBottom="@id/maxRangeLabel"/>

            <TextView android:id="@+id/minDelayLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/maxRangeLabel"
                android:layout_below="@id/maxRangeLabel"
                android:text="Min Delay:"
                android:layout_marginRight="5dip" />

            <TextView android:id="@+id/minDelay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/minDelayLabel"
                android:layout_alignTop="@id/minDelayLabel"
                android:layout_alignBottom="@id/minDelayLabel"/>

            <TextView android:id="@+id/powerLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/minDelayLabel"
                android:layout_below="@id/minDelayLabel"
                android:text="Power:"
                android:layout_marginRight="5dip" />

            <TextView android:id="@+id/power"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/powerLabel"
                android:layout_alignTop="@id/powerLabel"
                android:layout_alignBottom="@id/powerLabel"
                android:layout_marginRight="5dip"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/power"
                android:layout_alignTop="@id/power"
                android:layout_alignBottom="@id/power"
                android:text="mA"/>

            <TextView android:id="@+id/resolutionLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/powerLabel"
                android:layout_below="@id/powerLabel"
                android:text="Resolution:"
                android:layout_marginRight="5dip" />

            <TextView android:id="@+id/resolution"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/resolutionLabel"
                android:layout_alignTop="@id/resolutionLabel"
                android:layout_alignBottom="@id/resolutionLabel"/>

            <TextView android:id="@+id/vendorLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/resolutionLabel"
                android:layout_below="@id/resolutionLabel"
                android:text="Vendor:"
                android:layout_marginRight="5dip" />

            <TextView android:id="@+id/vendor"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/vendorLabel"
                android:layout_alignTop="@id/vendorLabel"
                android:layout_alignBottom="@id/vendorLabel"/>

            <TextView android:id="@+id/versionLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/versionLabel"
                android:layout_alignLeft="@id/vendorLabel"
                android:layout_below="@id/vendorLabel"
                android:text="Version:"
                android:layout_marginRight="5dip" />

            <TextView android:id="@+id/version"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/versionLabel"
                android:layout_alignTop="@id/versionLabel"
                android:layout_alignBottom="@id/versionLabel"/>

            <TextView android:id="@+id/accuracyLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/versionLabel"
                android:layout_below="@id/versionLabel"
                android:text="Accuracy:"
                android:layout_marginRight="5dip" />

            <TextView android:id="@+id/accuracy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/accuracyLabel"
                android:layout_alignTop="@id/accuracyLabel"
                android:layout_alignBottom="@id/accuracyLabel"/>

            <!-- timestamp -->
            <TextView android:id="@+id/timestampLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/accuracyLabel"
                android:layout_below="@id/accuracyLabel"
                android:layout_marginRight="5dip"
                android:text="Timestamp:" />

            <TextView android:id="@+id/timestamp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/timestampLabel"
                android:layout_alignTop="@id/timestampLabel"
                android:layout_alignBottom="@id/timestampLabel"
                android:layout_marginRight="5dip"
                android:visibility="gone" />

            <TextView android:id="@+id/timestampUnits"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/timestamp"
                android:layout_alignTop="@id/timestamp"
                android:layout_alignBottom="@id/timestamp"
                android:visibility="gone"
                android:text="(ns)" />

            <TextView android:id="@+id/dataLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/accuracyLabel"
                android:layout_below="@id/timestampLabel"
                android:layout_marginRight="5dip"
                android:visibility="gone"/>

            <TextView android:id="@+id/dataUnits"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/dataLabel"
                android:layout_alignTop="@id/dataLabel"
                android:layout_alignBottom="@id/dataLabel"
                android:visibility="gone" />

            <TextView android:id="@+id/singleValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/dataUnits"
                android:layout_alignTop="@id/dataUnits"
                android:layout_alignBottom="@id/dataUnits"
                android:visibility="gone" />

            <!-- X axis -->
            <TextView android:id="@+id/xAxisLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/dataLabel"
                android:layout_below="@id/dataLabel"
                android:layout_marginRight="5dip"
                android:visibility="gone"
                android:text="@string/xAxisLabel" />

            <TextView android:id="@+id/xAxis"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/xAxisLabel"
                android:layout_alignTop="@id/xAxisLabel"
                android:layout_alignBottom="@id/xAxisLabel"
                android:layout_marginRight="5dip"
                android:visibility="gone" />

            <!-- Y axis -->
            <TextView android:id="@+id/yAxisLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/xAxisLabel"
                android:layout_below="@id/xAxisLabel"
                android:layout_marginRight="5dip"
                android:visibility="gone"
                android:text="@string/yAxisLabel" />

            <TextView android:id="@+id/yAxis"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/yAxisLabel"
                android:layout_alignTop="@id/yAxisLabel"
                android:layout_alignBottom="@id/yAxisLabel"
                android:layout_marginRight="5dip"
                android:visibility="gone" />

            <!-- Z axis -->
            <TextView android:id="@+id/zAxisLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/yAxisLabel"
                android:layout_below="@id/yAxisLabel"
                android:layout_marginRight="5dip"
                android:visibility="gone"
                android:text="@string/zAxisLabel" />

            <TextView android:id="@+id/zAxis"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/zAxisLabel"
                android:layout_alignTop="@id/zAxisLabel"
                android:layout_alignBottom="@id/zAxisLabel"
                android:layout_marginRight="5dip"
                android:visibility="gone" />

            <!-- cos value (for rotation vector only) -->
            <TextView android:id="@+id/cosLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@id/zAxisLabel"
                android:layout_below="@id/zAxisLabel"
                android:layout_marginRight="5dip"
                android:visibility="gone"
                android:text="cos(\u0398/2):" />

            <TextView android:id="@+id/cos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/cosLabel"
                android:layout_alignTop="@id/cosLabel"
                android:layout_alignBottom="@id/cosLabel"
                android:layout_marginRight="5dip"
                android:visibility="gone" />

        </RelativeLayout>
    </ScrollView>

在没有看到您的清单的情况下,我无法100%回答这个问题,但我认为现在的情况是应用程序无法构建显示,因为它无法引用您的传感器片段。以下是SensorDisplayFragment的包声明:

package com.example.sensorlistactivity;
因此,要在xml文件中使用then,您应该将其作为类:

class="com.example.sensorlistactivity.SensorDisplayFragment" />
class="com.example.sensorlistactivity.SensorDisplayFragment" />