1

This is the first app I have ever tried to create, I am doing so for a college project. The main menu worked fine with the RelativeLayout when I was just using text buttons, but since I've switched to image buttons the app keeps crashing.

I don't have any error messages present in the code at all.

Here is the code I am using:

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ImageButton
    android:id="@+id/Button1"
    android:layout_width="135dp"
    android:layout_height="85dp"
    android:layout_alignParentTop="true"
    android:src="@drawable/test"
    android:contentDescription="@string/test" />

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/Button1"
    android:layout_below="@+id/Button1"
    android:layout_marginRight="55dp"
    android:text="@string/test" />    

<ImageButton
    android:id="@+id/Button2"
   android:layout_width="135dp"
    android:layout_height="85dp"       
    android:layout_alignParentRight="true"
    android:src="@drawable/depression101"
    android:contentDescription="@string/depression101" />

  <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Button2"
    android:layout_below="@+id/Button2"
    android:layout_marginLeft="20dp"
    android:text="@string/depression101" />


<ImageButton
    android:id="@+id/Button3"
    android:layout_width="135dp"
    android:layout_height="85dp"
    android:layout_alignLeft="@+id/Button1"
    android:layout_below="@+id/textView1"
    android:src="@drawable/tracker"
    android:contentDescription="@string/tracker" />

   <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/Button3"
    android:layout_below="@+id/Button3"
    android:layout_marginRight="45dp"
    android:text="@string/tracker" />

<ImageButton
    android:id="@+id/ImageButton4"
    android:layout_width="135dp"
    android:layout_height="85dp"
    android:layout_alignRight="@+id/Button2"
    android:layout_below="@+id/textView2"
    android:src="@drawable/progress"
    android:contentDescription="@string/progress" />

   <TextView
       android:id="@+id/textView4"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBaseline="@+id/textView3"
       android:layout_alignBottom="@+id/textView3"
       android:layout_alignRight="@+id/textView2"
       android:layout_marginRight="14dp"
       android:text="@string/progress" />

    <ImageButton
    android:id="@+id/Button5"
    android:layout_width="135dp"
    android:layout_height="85dp"
    android:layout_alignLeft="@+id/Button3"
    android:layout_below="@+id/textView3"
    android:src="@drawable/activesteps"
    android:contentDescription="@string/activeSteps"
    />

   <TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/Button5"
    android:layout_below="@+id/Button5"
    android:layout_marginRight="20dp"
    android:text="@string/activeSteps" />

    <ImageButton
       android:id="@+id/Button6"
       android:layout_width="135dp"
       android:layout_height="85dp"
       android:layout_above="@+id/textView5"
       android:layout_alignLeft="@+id/ImageButton4"
       android:src="@drawable/inspiration"
       android:contentDescription="@string/inspiration" />

   <TextView
       android:id="@+id/textView6"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        android:layout_alignRight="@+id/Button6"
        android:layout_below="@+id/Button6"
        android:layout_marginRight="30dp"
       android:text="@string/inspiration" />

   <ImageButton
       android:id="@+id/Button7"
       android:layout_width="135dp"
       android:layout_height="85dp"
       android:layout_alignLeft="@+id/Button5"
       android:layout_below="@+id/textView5"
       android:src="@drawable/usefullinks"
       android:contentDescription="@string/usefulLinks" 
       />

   <ImageButton
       android:id="@+id/ImageButton8"
       android:layout_width="135dp"
       android:layout_height="85dp"
       android:layout_alignLeft="@+id/Button6"
       android:layout_alignTop="@+id/Button7"
       android:src="@drawable/settings"
       android:contentDescription="@string/settings" />

   <TextView
       android:id="@+id/TextView7"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignLeft="@+id/textView5"
       android:layout_below="@+id/Button7"
       android:text="@string/usefulLinks" />

   <TextView
       android:id="@+id/TextView8"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBaseline="@+id/TextView7"
       android:layout_alignBottom="@+id/TextView7"
       android:layout_alignRight="@+id/textView4"
       android:text="@string/settings" />

</RelativeLayout>

Note: I know it's bad coding due to the use of Button8, etc. and lack of documentation but I'll get to that once it's working :)

MainActivity.java

package com.example.depressionapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //define test button
    final ImageButton trackerBtn = (ImageButton) findViewById(R.id.Button3);
    final ImageButton activeStepsBtn = (ImageButton) findViewById(R.id.Button5);

    trackerBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //listener call this function
            openTracker();
        }
    });

    activeStepsBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //listener call this function
            openActiveSteps();
        }
    });
   }

//Open tracker page
public void openTracker() {
    //create new textview
    Intent i = new Intent(getApplicationContext(), MoodTracker.class);
    startActivity(i);
 }

//Open active steps page
public void openActiveSteps() {
    //create new textview
    Intent i = new Intent(getApplicationContext(), ActiveSteps.class);
    startActivity(i);
}

}

Manifest:

package com.example.depressionapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.depressionapp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.depressionapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.example.depressionapp.MoodTracker"
        android:label="Mood Tracker" >
    </activity>

    <activity
        android:name="com.example.depressionapp.TreatmentTracker"
        android:label="Treatment Tracker" >
    </activity>

    <activity
        android:name="com.example.depressionapp.ActiveSteps"
        android:label="Active Steps" >
    </activity>

  </application>

</manifest>

My Log Cat looks like this:

12-12 14:18:07.230: D/AndroidRuntime(1329): Shutting down VM
12-12 14:18:07.230: W/dalvikvm(1329): threadid=1: thread exiting with uncaught exception (group=0xb4a26b90)
12-12 14:18:07.290: E/AndroidRuntime(1329): FATAL EXCEPTION: main
12-12 14:18:07.290: E/AndroidRuntime(1329): Process: com.example.depressionapp, PID: 1329
12-12 14:18:07.290: E/AndroidRuntime(1329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.depressionapp/com.example.depressionapp.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
12-12 14:18:07.290: E/AndroidRuntime(1329):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at android.os.Looper.loop(Looper.java:137)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at java.lang.reflect.Method.invoke(Method.java:515)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at dalvik.system.NativeStart.main(Native Method)
12-12 14:18:07.290: E/AndroidRuntime(1329): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
12-12 14:18:07.290: E/AndroidRuntime(1329):     at com.example.depressionapp.MainActivity.onCreate(MainActivity.java:18)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at android.app.Activity.performCreate(Activity.java:5243)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-12 14:18:07.290: E/AndroidRuntime(1329):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-12 14:18:07.290: E/AndroidRuntime(1329):     ... 11 more
Kara
  • 6,115
  • 16
  • 50
  • 57
Ashley Keating
  • 119
  • 1
  • 5
  • 12

2 Answers2

1

Change these lines...

final Button trackerBtn = (Button) findViewById(R.id.Button3);
final Button activeStepsBtn = (Button) findViewById(R.id.Button5);

...to...

final ImageButton trackerBtn = (ImageButton) findViewById(R.id.Button3);
final ImageButton activeStepsBtn = (ImageButton) findViewById(R.id.Button5);

You can't cast an ImageButton to a Button and both Button3 and Button5 are defined as ImageButtons in the XML layout. Also fix the issue with @+id/space I mentioned in my comment.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Thank you. However, I have done all of those things and also fixed all of the warnings. And the app is still crashing. – Ashley Keating Dec 12 '13 at 20:14
  • @user3096838 : In that case, it is crashing for some new reason. Your logcat clearly shows the original crash was a `java.lang.ClassCastException` (see the "Caused by:" line). If it's still crashing then something else is wrong and you need to look at your logcat again. – Squonk Dec 12 '13 at 20:32
  • @user3096838 : Good to hear, glad to help. – Squonk Dec 12 '13 at 22:10
0

Based on your updated question with the LogCat & java file: it seems that you are instantiating the "Button3" as a "Button" but it is really an "ImageButton"

<ImageButton
android:id="@+id/Button5"
android:layout_width="135dp"
android:layout_height="85dp"
android:layout_alignLeft="@+id/Button3"
android:layout_below="@+id/textView3"
android:src="@drawable/activesteps" />

You have the same issue for Button5

user1406716
  • 9,565
  • 22
  • 96
  • 151