在Android中为第二个活动添加了一个按钮以返回到第一个。现在应用程序在导航到第二个活动时崩溃

时间:2013-12-13 15:38:42

标签: java android xml button crash

我的应用有点麻烦。我正处于制作游戏的开始阶段,我有一个运行正常的简单说明页面。直到我在底部添加了一个按钮才能返回主页。现在当我点击指示页面的链接时,应用程序崩溃了。我环顾四周,只能找到那些没有将活动添加到我所做的清单中的人。请帮忙......

要返回的按钮的代码。

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;

public class HowTo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 
setContentView(R.layout.how_to);

Button btnHomeFromHow = (Button) findViewById(R.id.btnHomeFromHow);
btnHomeFromHow.setOnClickListener(new OnClickListener() {


    public void onClick(View view) {
        startActivity(new Intent(HowTo.this, Home.class));
    }
});

清单。

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.whackachav.Home"
        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.whackachav.HowTo"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.Main" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        </activity>

    <activity 
        android:name="com.example.whackachav.Game"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity 
        android:name="com.example.whackachav.HighScores"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>   
    </activity>
</application>

</manifest>

布局文件中的按钮。

        <button
        android:id="@+id/btnHomeFromHow"
        style="@style/btnStyleShakespeare"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvBucky"
        android:text="Back to Main Menu"
        android:layout_marginTop="10dp"
        />

日志。

12-13 16:05:25.560: E/Trace(2397): error opening trace file: No such file or directory (2)
12-13 16:05:26.260: D/dalvikvm(2397): GC_FOR_ALLOC freed 53K, 8% free 2456K/2652K, paused 42ms, total 44ms
12-13 16:05:26.431: D/dalvikvm(2397): GC_CONCURRENT freed 2K, 7% free 2909K/3112K, paused 82ms+4ms, total 156ms
12-13 16:05:27.100: D/gralloc_goldfish(2397): Emulator without GPU emulation detected.
12-13 16:05:27.740: I/Choreographer(2397): Skipped 84 frames!  The application may be doing too much work on its main thread.
12-13 16:05:35.921: D/dalvikvm(2397): GC_CONCURRENT freed 30K, 5% free 3349K/3524K, paused 79ms+8ms, total 210ms
12-13 16:05:35.921: D/dalvikvm(2397): WAIT_FOR_CONCURRENT_GC blocked 39ms
12-13 16:05:36.160: D/dalvikvm(2397): GC_FOR_ALLOC freed 50K, 6% free 3546K/3772K, paused 47ms, total 55ms
12-13 16:05:36.170: I/dalvikvm-heap(2397): Grow heap (frag case) to 4.213MB for 643068-byte allocation
12-13 16:05:36.240: D/dalvikvm(2397): GC_FOR_ALLOC freed <1K, 6% free 4174K/4404K, paused 64ms, total 64ms
12-13 16:05:36.360: D/dalvikvm(2397): GC_CONCURRENT freed <1K, 6% free 4174K/4404K, paused 5ms+17ms, total 117ms
12-13 16:05:36.721: D/dalvikvm(2397): GC_FOR_ALLOC freed <1K, 6% free 4178K/4404K, paused 44ms, total 52ms
12-13 16:05:36.740: I/dalvikvm-heap(2397): Grow heap (frag case) to 4.942MB for 760600-byte allocation
12-13 16:05:36.950: D/dalvikvm(2397): GC_CONCURRENT freed <1K, 5% free 4920K/5148K, paused 85ms+4ms, total 210ms
12-13 16:05:37.330: D/AndroidRuntime(2397): Shutting down VM
12-13 16:05:37.340: W/dalvikvm(2397): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-13 16:05:37.500: E/AndroidRuntime(2397): FATAL EXCEPTION: main
12-13 16:05:37.500: E/AndroidRuntime(2397): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.whackachav/com.example.whackachav.HowTo}: android.view.InflateException: Binary XML file line #101: Error inflating class button
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.os.Looper.loop(Looper.java:137)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at java.lang.reflect.Method.invokeNative(Native Method)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at java.lang.reflect.Method.invoke(Method.java:511)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at dalvik.system.NativeStart.main(Native Method)
12-13 16:05:37.500: E/AndroidRuntime(2397): Caused by: android.view.InflateException: Binary XML file line #101: Error inflating class button
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.app.Activity.setContentView(Activity.java:1881)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at com.example.whackachav.HowTo.onCreate(HowTo.java:15)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.app.Activity.performCreate(Activity.java:5104)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-13 16:05:37.500: E/AndroidRuntime(2397):     ... 11 more
12-13 16:05:37.500: E/AndroidRuntime(2397): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.button" on path: /data/app/com.example.whackachav-1.apk
12-13 16:05:37.500: E/AndroidRuntime(2397):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.view.LayoutInflater.createView(LayoutInflater.java:552)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
12-13 16:05:37.500: E/AndroidRuntime(2397):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
12-13 16:05:37.500: E/AndroidRuntime(2397):     ... 22 more
12-13 16:05:39.780: I/Process(2397): Sending signal. PID: 2397 SIG: 9

4 个答案:

答案 0 :(得分:0)

您没有在Android Manifest中定义Home.java类。  试试这个。

<activity 
    android:name="com.example.whackachav.Home"
    android:label="@string/app_name" >
    </activity>

答案 1 :(得分:0)

你应该使用..

“android.intent.category.DEFAULT”

for HowTo

如果您使用LAUNCHER for Home

答案 2 :(得分:0)

更改

<button
        android:id="@+id/btnHomeFromHow"
        style="@style/btnStyleShakespeare"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvBucky"
        android:text="Back to Main Menu"
        android:layout_marginTop="10dp"
        />

<Button  // B in caps
    android:id="@+id/btnHomeFromHow"
    style="@style/btnStyleShakespeare"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/tvBucky"
    android:text="Back to Main Menu"
    android:layout_marginTop="10dp"
    />

答案 3 :(得分:0)

你的问题可能就在这里:

java.lang.ClassNotFoundException: Didn't find class "android.view.button" on path: /data/app/com.example.whackachav-1.apk

可能由以下原因引起:

<button
        android:id="@+id/btnHomeFromHow"
        style="@style/btnStyleShakespeare"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvBucky"
        android:text="Back to Main Menu"
        android:layout_marginTop="10dp"
        />

因为“按钮”实际上应该是带有大写字母B的“按钮”。

此外,可能不相关,但根据您启动“HowTo”活动的方式,最好只需拨打finish()而不是尝试再次启动Home活动。