Question

I have just started learning android. I have develop a program named Android Light Sensor that measure the Intensity of Light. Here is my code:

    package com.AndroidLightSensor;

import com.example.andriodlightsensor.R;

import android.os.Bundle;
import android.app.Activity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class AndriodLightSensorActivity extends Activity {

    ProgressBar lightMeter;
     TextView textMax, textReading;
     float counter;
     Button read;
     TextView display;

        /** Called when the activity is first created. */

     @Override
        public void onCreate(Bundle savedInstanceState) {
         counter = 0;
         read = (Button) findViewById(R.id.bStart);
         display = (TextView) findViewById(R.id.tvDisplay);

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            lightMeter = (ProgressBar)findViewById(R.id.lightmeter);
            textMax = (TextView)findViewById(R.id.max);
            textReading = (TextView)findViewById(R.id.reading);

            SensorManager sensorManager 
            = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
            Sensor lightSensor 
            = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

            if (lightSensor == null){
             Toast.makeText(AndriodLightSensorActivity.this, 
               "No Light Sensor! quit-", 
               Toast.LENGTH_LONG).show();
            }else{
             float max =  lightSensor.getMaximumRange();
             lightMeter.setMax((int)max);
             textMax.setText("Max Reading(Lux): " + String.valueOf(max));

             sensorManager.registerListener(lightSensorEventListener, 
               lightSensor, 
               SensorManager.SENSOR_DELAY_NORMAL);

            }
        }

     SensorEventListener lightSensorEventListener
        = new SensorEventListener(){

      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
       // TODO Auto-generated method stub

      }

      @Override
      public void onSensorChanged(SensorEvent event) {
       // TODO Auto-generated method stub
       if(event.sensor.getType()==Sensor.TYPE_LIGHT){
        final float currentReading = event.values[0];
        lightMeter.setProgress((int)currentReading);
        textReading.setText("Current Reading(Lux): " + String.valueOf(currentReading));
        read.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                display.setText("" + String.valueOf(currentReading));
            }
        });

       }
      }

        };
    }

Also The xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tvDisplay"

    />
<ProgressBar
    android:id="@+id/lightmeter"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="80dp"
    style="?android:attr/progressBarStyleHorizontal"
    android:max="100"
    android:progress="0"
    />
<TextView 
    android:id="@+id/max"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    />
<TextView 
    android:id="@+id/reading"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 

    />
<Button 
    android:layout_width="250dp" 
    android:layout_height="wrap_content"
    android:text="Start"  
    android:layout_y="249dp"  
    android:textSize="30dp"
    android:onClick="onButtonDown"
    android:id="@+id/bStart" 
    ></Button>

</LinearLayout>

I want to get the current value whenever I press the button; i:e the value should not keep changing (Like in Stop Watch, but the updated should replace the previous one). In Eclipse, It shows no error, but when I run on my device is says" Unfortunately, Android Light Sensor has stopped." Please help!!

Était-ce utile?

La solution

There are an error that is obvious.
If findViewById is used before setContentView(R.layout.main); the values returned are null.
When you try to use them you get an error.

Put this two lines after setContentView(R.layout.main);

read = (Button) findViewById(R.id.bStart);
display = (TextView) findViewById(R.id.tvDisplay);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top