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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25711431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:31:20  来源:igfitidea点击:

Simple android app crashes on startup

javaandroidxmlcrashstartup

提问by antoninkriz

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

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

Here is the code:

这是代码:

Arcig Manifest.xml

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

活动_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

主活动.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

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

回答by laalto

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

and

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

ImageButtonis not a Buttonso this causes ClassCastExceptionyou would have seen in your logcat if you had included it in the question. (Hint: Always start crash-solving with the stacktrace in logcat.)

ImageButton不是,Button所以ClassCastException如果您将它包含在问题中,这会导致您在 logcat 中看到它。(提示:始终使用 logcat 中的堆栈跟踪开始解决崩溃问题。)

Change the latter part to

将后半部分改为

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

回答by Panda1667075

I can see you are using ImageButton. I guess, the drawable images source size are more. If the images size are more android app will crash. Please try to keep your individual image size as low as possible. May be with in 50-60KB per image.

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