0

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

Chris Panella
  • 202
  • 1
  • 9
  • try Project -> Clean, sometimes resources don't get built properly – samgak Mar 10 '15 at 01:41
  • I have recleaned/rebuilt the project. My Logcat isn't showing anything except "Waiting for debugger to settle..." several times followed by "com.yourfuturehero.lightsplantgrow D/dalvikvm﹕ threadid=1: still suspended after undo (sc=1 dc=1)". Also I am running it on a Galaxy S4 vs using a virtual device. – Chris Panella Mar 10 '15 at 03:09
  • If your imageview is `centerInParent` what is the need to use `layout_above`? –  Mar 10 '15 at 20:26
  • @Gaurav I was using the GUI to build the layout and that is what Android Studio put in for me. I assume it's because I have a RelativeLayout and I added the center part. However, even if I only use one or the other, the app still crashes during launch. – Chris Panella Mar 10 '15 at 22:52

2 Answers2

0

I think you must write

android:layout_above="@id/light_available"

Instead of

android:layout_above="@+id/light_available"

I do not know how to use layout_above, but it is strange to see @+id twice.

EDIT 1 : As I said in the comment

  • @id/someId references an existing id in your project
  • @+id/someId creates a new id in your project

I think you must change your code to

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

If this answer is not working, here is a thread with an alternative idea. (You will notice the use of @id with android:layout_above)

EDIT 2 : This is a new code (I tested this one and it works for me).

Under the title, which is a textView, is an imageView, followed by two more text views.

<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:id="@+id/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:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_below="@id/textView"
        android:layout_margin="10dp"
        android:src="@drawable/homeplant" />

    <TextView
        android:id="@+id/light_available"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Sensor"
        android:layout_below="@id/imageView"
        android:layout_marginBottom="35dp"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/light_reading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Light"
        android:layout_marginBottom="43dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/light_available"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

It is easier to put the elements in the order you want to display them. In this case you can also use a LinearLayout. Here is the code

<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"
    android:background="#ffffffff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:text="@string/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textIsSelectable="false"
        android:textSize="@dimen/abc_text_size_headline_material"
        android:textColor="#ff2db81a"
        android:gravity="center"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_below="@id/textView"
        android:layout_margin="10dp"
        android:src="@drawable/homeplant"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/light_available"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Sensor"
        android:layout_below="@id/imageView"
        android:layout_marginBottom="35dp"
        android:gravity="center" />

    <TextView
        android:id="@+id/light_reading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Light"
        android:layout_marginBottom="43dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/light_available"
        android:layout_centerHorizontal="true"
        android:gravity="center"/>

</LinearLayout>

EDIT 3 :

public class MainActivity extends ActionBarActivity implements SensorEventListener {

    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(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
        } else {
            textLightIsAvailable.setText("You Don't Have a Sensor!");
        }
    }

    @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]);
        }
    }
}
Community
  • 1
  • 1
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
  • If you are suggesting to just change the imageView, I tried that and I still get a crash. I am not sure what the + does, Android Studio added that code in automatically when I added the imageView in the Graphical Design UI. – Chris Panella Mar 10 '15 at 19:07
  • Just try to remove the + in the ImageView. The + tells android to create a new id, but you want to use an existing id, not create a new one. I will add more details in my answer, please see the edit. – ThomasThiebaud Mar 10 '15 at 19:25
  • I tried your code and I get a different error before the app even launches... Error:(27, 31) No resource found that matches the given name (at 'layout_above' with value '@id/light_reading')... Alternatively, I attempted the other threads idea. That other thread makes sense but it still crashes. (I have tried starting the project over from scratch before as well). I will continue to play with moving around the layout, I just wish the logcat was showing more information. I appreciate your input thus far! – Chris Panella Mar 10 '15 at 21:18
  • Yes this is normal. You write `android:layout_above="@id/light_reading"` but there is no element with the id light_reading above. See new edit. – ThomasThiebaud Mar 11 '15 at 07:59
  • There weren't any errors that time but it still crashes when it launches. Could it be a bad imageView file or something? I already tried re-installing Android Studio. I am on a mac too in case that makes a difference. – Chris Panella Mar 11 '15 at 15:05
  • I am also working on a mac. You have some errors in your .java file. I will post an edit soon. – ThomasThiebaud Mar 11 '15 at 17:12
  • I am still getting a crash during launch. I added the JRE version and AS version. Not sure if that will help. Thank you again. – Chris Panella Mar 12 '15 at 17:57
0

It looks like the image I was using was a bad file or something. I am not sure, but as soon as I changed pictures the app began working. I had a baby plant picture that was a JPEG file, every other picture I have in my /drawable resource file works besides the one labelled as "homeplant". I also had a few error's in my code that Maloubobola helped with.

Thank you!!!

Chris Panella
  • 202
  • 1
  • 9