[英]Added A Button To Second Activity In Android To Return To First. Now App Crashes When Navigating To Second Activity
Having a bit of trouble with my app. 我的应用有点麻烦。 I'm in the beginning stages of making a game and I have a simple instructions page that was running fine.
我正处于制作游戏的起步阶段,并且有一个简单的说明页面,运行正常。 That is until I added a button at the bottom to return to the homepage.
直到我在底部添加了一个按钮以返回首页。 Now when I click the link to the instructions page the app crashes.
现在,当我单击指向说明页面的链接时,应用程序崩溃了。 I looked around and can only find people who haven't added the activity to the manifest which I have done.
我环顾四周,只能找到没有将活动添加到清单中的人。 Please help...
请帮忙...
Code for button to return. 按钮返回的代码。
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));
}
});
Manifest. 表现。
<?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 in layout file. 布局文件中的按钮。
<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"
/>
log. 登录。
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
You didn't define the Home.java class in Android Manifest. 您没有在Android Manifest中定义Home.java类。 so try this.
所以试试看
<activity
android:name="com.example.whackachav.Home"
android:label="@string/app_name" >
</activity>
you should use.. 您应该使用..
"android.intent.category.DEFAULT" “android.intent.category.DEFAULT”
for HowTo 对于如何
if you are using LAUNCHER for Home 如果您正在使用LAUNCHER for Home
Change 更改
<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"
/>
To 至
<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"
/>
Your problem might be here: 您的问题可能在这里:
java.lang.ClassNotFoundException: Didn't find class "android.view.button" on path: /data/app/com.example.whackachav-1.apk
which is likely caused by: 这可能是由于:
<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"
/>
Because "button" should actually be "Button" with a capital B. 因为“按钮”实际上应该是带有大写字母B的“按钮”。
Also, possibly unrelated, but depending on how you started the "HowTo" activity, it may be better to simply call finish() instead of trying to start the Home activity again. 另外,可能不相关,但是取决于您如何启动“ HowTo”活动,最好直接调用finish()而不是尝试再次启动Home活动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.