Java 在Android的第二个活动中添加了一个按钮以返回第一个活动。现在,当导航到第二个活动时,应用程序崩溃
我的应用程序有点问题。我正处于制作游戏的开始阶段,我有一个简单的说明页面,运行良好。直到我在底部添加了一个按钮返回主页。现在,当我单击指向“说明”页面的链接时,应用程序崩溃。我环顾四周,只能找到那些没有将活动添加到清单中的人。请帮忙 返回按钮的代码Java 在Android的第二个活动中添加了一个按钮以返回第一个活动。现在,当导航到第二个活动时,应用程序崩溃,java,android,xml,button,crash,Java,Android,Xml,Button,Crash,我的应用程序有点问题。我正处于制作游戏的开始阶段,我有一个简单的说明页面,运行良好。直到我在底部添加了一个按钮返回主页。现在,当我单击指向“说明”页面的链接时,应用程序崩溃。我环顾四周,只能找到那些没有将活动添加到清单中的人。请帮忙 返回按钮的代码 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; im
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"
/>
日志
您没有在Android清单中定义Home.java类。 所以试试这个
<activity
android:name="com.example.whackachav.Home"
android:label="@string/app_name" >
</activity>
你应该用
android.intent.category.DEFAULT
关于如何
如果您使用的是Home启动器更改
<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"
/>
到
您的问题可能在这里:
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活动,最好是直接调用,而不是再次尝试启动Home活动。您是否在清单中声明了Home活动?是的,可能应该提到这一点。Home是我开始时创建的第一个活动,因此默认情况下它位于其中。您的崩溃日志在哪里?@user3099929使用HomeTo.this而不是getApplicationContext,并确保按钮位于how_to.xml中,以及您为什么将HomeTo设置为启动器?对不起,我是android开发新手。你到底在找什么?
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"
/>