新手应用程序错误显示传感器信息

时间:2015-05-28 07:38:07

标签: android sensor

仍然是新手,无法解决我的问题。这是一个显示光传感器值的简单应用程序。但是当我试图运行应用程序时,我收到了一个错误。

主要活动:

    package za.co.litedata.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.widget.TextView;
import android.content.Context;
import android.os.Build;


public class MainActivity extends ActionBarActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        SensorManager mySensorManager;
        Sensor myLightSensor;

        public PlaceholderFragment() {
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView Text1 = (TextView)rootView.findViewById(R.id.lightsensor);

            mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
            myLightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

            if (myLightSensor == null){
                Text1.setText("No Light Sensor!");
            }else{
                //Text1.setText(myLightSensor.getName());
                Text1.setText("Have Sensor");
            }
            return rootView;
        }
    }
}

这是碎片:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment">


    <TextView
        android:id="@+id/lightsensor"
        android:layout_width="fill_parent"
        android:layout_height="49dp"
        />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

好吧,似乎我发现了问题)将TextView的初始文本剪切并粘贴到片段的OnCreateView中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    TextView Text1 = (TextView)rootView.findViewById(R.id.lightsensor);

    mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    myLightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

    if (myLightSensor == null){
        Text1.setText("No Light Sensor!");
    }else{
        //Text1.setText(myLightSensor.getName());
        Text1.setText("Have Sensor");
    }
    return rootView;
}