简单的Android应用程序在启动时崩溃

时间:2014-09-07 14:46:57

标签: java android xml crash startup

我正在创建一个新的Android应用,但它在启动时崩溃(手机和模拟器)。

以下是代码:

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 }

当我执行应用程序时,它只显示崩溃消息。 :(任何人都可以帮助我吗?我在代码中犯了什么错误? 非常感谢你们

2 个答案:

答案 0 :(得分:7)

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

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

ImageButton不是Button因此,如果您将其包含在问题中,则会导致您在logcat中看到ClassCastException。 (提示:始终使用logcat中的stacktrace开始崩溃解决。)

将后一部分改为

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

答案 1 :(得分:0)

我可以看到你正在使用ImageButton。我想,可绘制的图像源尺寸更大。如果图像大小更多Android应用程序将崩溃。请尽量保持您个人的图片尺寸尽可能低。每张图片可能有50-60KB。