[英]Simple android app crashes on 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();
}
});
}
}
安慰
[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 }
当我执行该应用程序时,它仅显示崩溃消息。 :(有人可以帮我吗?我在代码中犯了任何错误吗?非常感谢
<ImageButton
android:id="@+id/imageButton1"
和
Button button = (Button) findViewById(R.id.imageButton1);
ImageButton
不是Button
因此如果您将LogCastException包含在问题中,则会导致ClassCastException
。 (提示:始终使用logcat中的stacktrace开始崩溃解决。)
将后半部分更改为
ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
我可以看到您正在使用ImageButton。 我想,可绘制图像源的大小更多。 如果图像尺寸更大,android应用程序将崩溃。 请尝试使个人图像尺寸尽可能小。 每个图片可能包含50-60KB。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.