I am still fairly new to android development so any help is truly appreciated.
I have an app that uses the light sensor to read the amount of light and display it on the screen. I am developing it on android studio. Under the title, which is a textView, is an imageView, followed by two more text views. No matter which layout I try the app keeps crashing when I have an imageView. As soon as I remove the imageView the app will run without any problems. My image is in the drawable package(hope that is the correct term) and is a JPEG (which according to the documents is a supported file type).
Also I understand that one of my textView items is set equal to "Light", versus pointing at a string. I am aware that this is not the way I should do it, I am still working on the app.
Please help me figure out what I am doing wrong. I appreciate all of the help and time!
<RelativeLayout 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"
android:background="#ffffffff">
<TextView android:text="@string/title" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textIsSelectable="false"
android:textSize="@dimen/abc_text_size_headline_material"
android:textColor="#ff2db81a"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/light_available"
android:text="Sensor"
android:layout_marginBottom="35dp"
android:layout_above="@+id/light_reading"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/light_reading"
android:text="Light"
android:layout_marginBottom="43dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/imageView"
android:layout_above="@+id/light_available"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:src="@drawable/homeplant" />
Here is the Java...
public class MainActivity extends ActionBarActivity {
TextView textLightIsAvailable, textLightReading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLightIsAvailable = (TextView)findViewById(R.id.light_available);
textLightReading = (TextView)findViewById(R.id.light_reading);
SensorManager mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor LightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if(LightSensor != null){
textLightIsAvailable.setText("You Have a Sensor!");
mySensorManager.registerListener(LightSensorListener, LightSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
else{
textLightIsAvailable.setText("You Don't Have a Sensor!");
}
}
private final SensorEventListener LightSensorListener= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_LIGHT){
textLightReading.setText("LIGHT: " + event.values[0]);
}
}
};
Update: 3/10/15: I have tried two different virtual devices as well with the same error as my physical device. The logcat only shows(I took out the time stamps/mypackage name because of how it looked formatted on here)...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ debugger has settled (1433)
Thanks again.
Edit: 3/12/15
My JRE version is - 1.6.0_65-B14-466.1 11M4716 x86_64 Android Studio version - 1.1.0