问题描述:

Still a newbie and cannot solve my problem. Here is a simple app displaying the light sensor value. But when trying to run the app I am getting a error.

Main Activity:

 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;

}

}

}

Here is the Fragment:

<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>

网友答案:

ok, it seems I found the problem) cut and paste TextView's init text in fragment's 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;
}
相关阅读:
Top