Search code examples
javaandroidxmlcrashstartup

Simple android app crashes on startup


I am creating a new Android app, but it crashes on startup (phone and emulator).

Here is the code:

Arcig Manifest.xml

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

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="17" /><application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
    <activity android:name="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>
</application>

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="suplovani">Suplování</string>
    <string name="rozvrh">Rozvrh hodin</string>
    <string name="prihlasovani">Přihlašování do systému</string>
    <string name="email">Email</string>
    <string name="moodle">Moodle</string>
    <string name="kdm">KDM</string>
    <string name="o_aplikaci">O Aplikaci</string>
    <string name="app_name">Arcig.CZ</string>

</resources>

activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:contentDescription="@string/suplovani"
            android:src="@drawable/suplovani" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:contentDescription="@string/rozvrh"
            android:src="@drawable/rozvrh" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:contentDescription="@string/prihlasovani"
            android:src="@drawable/prihlasovani" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:contentDescription="@string/email"
            android:src="@drawable/email" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:contentDescription="@string/email"
            android:src="@drawable/moodle" />

        <ImageButton
            android:id="@+id/imageButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:contentDescription="@string/kdm"
            android:src="@drawable/jidelna" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/imageButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/o_aplikaci"
            android:src="@drawable/about" />

    </LinearLayout>

</LinearLayout>

MainActivity.java

package com.tona.arcig;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        addListenerOnButton();
    }

    public void addListenerOnButton() {

        Button button = (Button) findViewById(R.id.imageButton1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Do something in response to button click
                Toast.makeText(getApplicationContext(), "Mam rad vlaky", Toast.LENGTH_LONG).show();
            }
        });
    }
}

CONSOLE

[2014-09-07 16:39:21 - Arcig] ------------------------------
[2014-09-07 16:39:21 - Arcig] Android Launch!
[2014-09-07 16:39:21 - Arcig] adb is running normally.
[2014-09-07 16:39:21 - Arcig] Performing com.tona.arcig.MainActivity activity launch
[2014-09-07 16:39:21 - Arcig] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2014-09-07 16:39:23 - Arcig] Application already deployed. No need to reinstall.
[2014-09-07 16:39:23 - Arcig] Starting activity com.tona.arcig.MainActivity on device 0123456789ABCDEFG
[2014-09-07 16:39:24 - Arcig] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.tona.arcig/.MainActivity }

When I execute the app, it just shows a crash message. :( Can anybody help me? Have I made any mistakes in the code? Thanks a lot guys


Solution

  • <ImageButton
            android:id="@+id/imageButton1"
    

    and

    Button button = (Button) findViewById(R.id.imageButton1);
    

    ImageButton is not a Button so this causes ClassCastException you would have seen in your logcat if you had included it in the question. (Hint: Always start crash-solving with the stacktrace in logcat.)

    Change the latter part to

    ImageButton button = (ImageButton) findViewById(R.id.imageButton1);