这是我尝试创建的第一个应用程序,我正在为一个大学项目这样做。当我刚刚使用文本按钮时,主菜单与RelativeLayout工作正常,但由于我已切换到图像按钮,应用程序不断崩溃。
我根本没有任何错误消息。
以下是我正在使用的代码:
<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>
注意:我知道由于使用Button8等而导致编码错误,而且缺少文档,但是一旦它正常工作我就会知道:)
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);
}
}
清单:
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>
我的Log Cat看起来像这样:
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
答案 0 :(得分:1)
更改这些行......
final Button trackerBtn = (Button) findViewById(R.id.Button3);
final Button activeStepsBtn = (Button) findViewById(R.id.Button5);
...到...
final ImageButton trackerBtn = (ImageButton) findViewById(R.id.Button3);
final ImageButton activeStepsBtn = (ImageButton) findViewById(R.id.Button5);
您无法将ImageButton
转换为Button
,并且Button3和Button5都在XML布局中定义为ImageButtons
。同时解决我在评论中提到的@+id/space
问题。
答案 1 :(得分:0)
根据您使用LogCat&amp; amp;更新的问题java文件:似乎你正在实例化&#34; Button3&#34;作为&#34;按钮&#34;但它确实是一个&#34; ImageButton&#34;
<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" />
您对Button5
有同样的问题