Android 应用程序在按钮单击时崩溃
您好,我编写了一个带有两个 xml 页面的简单 android 应用程序,其中一个有一个图像按钮,我将其设置为在单击时打开第二个 xml 页面。该代码没有返回错误并且运行良好,但是当我单击 android 模拟器中的按钮时,它崩溃并要求我强制关闭。这是我正在处理的代码:
第一个活动:
package com.android.Train;
import com.android.TrainMenu.MenuActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class TrainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void goMenu(View v)
{
Intent myIntent = new Intent(this, MenuActivity.class);
startActivity(myIntent);
}
}
第一个 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_enter"
android:clickable="true"
android:onClick="goMenu" />
</LinearLayout>
第二个活动:
package com.android.TrainMenu;
import android.app.Activity;
import android.os.Bundle;
import com.android.Train.R;
public class MenuActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
}
}
第二个 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
关于为什么会崩溃的任何想法?
预先感谢
我刚刚再次运行它,这是 logCat 文件
01-26 14:21:37.784: D/AndroidRuntime(287): Shutting down VM
01-26 14:21:37.784: W/dalvikvm(287): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-26 14:21:37.815: E/AndroidRuntime(287): FATAL EXCEPTION: main
01-26 14:21:37.815: E/AndroidRuntime(287): java.lang.IllegalStateException: Could not execute method of the activity
01-26 14:21:37.815: E/AndroidRuntime(287): at android.view.View$1.onClick(View.java:2072)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.view.View.performClick(View.java:2408)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.view.View$PerformClick.run(View.java:8816)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.os.Handler.handleCallback(Handler.java:587)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.os.Looper.loop(Looper.java:123)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-26 14:21:37.815: E/AndroidRuntime(287): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 14:21:37.815: E/AndroidRuntime(287): at java.lang.reflect.Method.invoke(Method.java:521)
01-26 14:21:37.815: E/AndroidRuntime(287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-26 14:21:37.815: E/AndroidRuntime(287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-26 14:21:37.815: E/AndroidRuntime(287): at dalvik.system.NativeStart.main(Native Method)
01-26 14:21:37.815: E/AndroidRuntime(287): Caused by: java.lang.reflect.InvocationTargetException
01-26 14:21:37.815: E/AndroidRuntime(287): at com.android.Train.TrainActivity.goMenu(TrainActivity.java:23)
01-26 14:21:37.815: E/AndroidRuntime(287): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 14:21:37.815: E/AndroidRuntime(287): at java.lang.reflect.Method.invoke(Method.java:521)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.view.View$1.onClick(View.java:2067)
01-26 14:21:37.815: E/AndroidRuntime(287): ... 11 more
01-26 14:21:37.815: E/AndroidRuntime(287): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.Train/com.android.TrainMenu.MenuActivity}; have you declared this activity in your AndroidManifest.xml?
01-26 14:21:37.815: E/AndroidRuntime(287): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.app.Activity.startActivityForResult(Activity.java:2817)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.app.Activity.startActivity(Activity.java:2923)
01-26 14:21:37.815: E/AndroidRuntime(287): ... 15 more
Android Manifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.Train"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".TrainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MenuActivity"
android:label="@string/app_name" />
</application>
</manifest>
Hi Ive written a simple android application with two xml pages, one has an image button which ive rigged to open the second xml page when it is clicked. The code returns no errors and runs fine but when I click the button in the android emulator it crashes and asks me to force close. Here is the code I am working on:
First Activity:
package com.android.Train;
import com.android.TrainMenu.MenuActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class TrainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void goMenu(View v)
{
Intent myIntent = new Intent(this, MenuActivity.class);
startActivity(myIntent);
}
}
First XML File:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="@+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_enter"
android:clickable="true"
android:onClick="goMenu" />
</LinearLayout>
Second Activity:
package com.android.TrainMenu;
import android.app.Activity;
import android.os.Bundle;
import com.android.Train.R;
public class MenuActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
}
}
Second XML File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
Any ideas on why this should crash?
Thanks in advance
I just ran it again, here is the logCat file
01-26 14:21:37.784: D/AndroidRuntime(287): Shutting down VM
01-26 14:21:37.784: W/dalvikvm(287): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-26 14:21:37.815: E/AndroidRuntime(287): FATAL EXCEPTION: main
01-26 14:21:37.815: E/AndroidRuntime(287): java.lang.IllegalStateException: Could not execute method of the activity
01-26 14:21:37.815: E/AndroidRuntime(287): at android.view.View$1.onClick(View.java:2072)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.view.View.performClick(View.java:2408)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.view.View$PerformClick.run(View.java:8816)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.os.Handler.handleCallback(Handler.java:587)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.os.Looper.loop(Looper.java:123)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-26 14:21:37.815: E/AndroidRuntime(287): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 14:21:37.815: E/AndroidRuntime(287): at java.lang.reflect.Method.invoke(Method.java:521)
01-26 14:21:37.815: E/AndroidRuntime(287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-26 14:21:37.815: E/AndroidRuntime(287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-26 14:21:37.815: E/AndroidRuntime(287): at dalvik.system.NativeStart.main(Native Method)
01-26 14:21:37.815: E/AndroidRuntime(287): Caused by: java.lang.reflect.InvocationTargetException
01-26 14:21:37.815: E/AndroidRuntime(287): at com.android.Train.TrainActivity.goMenu(TrainActivity.java:23)
01-26 14:21:37.815: E/AndroidRuntime(287): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 14:21:37.815: E/AndroidRuntime(287): at java.lang.reflect.Method.invoke(Method.java:521)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.view.View$1.onClick(View.java:2067)
01-26 14:21:37.815: E/AndroidRuntime(287): ... 11 more
01-26 14:21:37.815: E/AndroidRuntime(287): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.Train/com.android.TrainMenu.MenuActivity}; have you declared this activity in your AndroidManifest.xml?
01-26 14:21:37.815: E/AndroidRuntime(287): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.app.Activity.startActivityForResult(Activity.java:2817)
01-26 14:21:37.815: E/AndroidRuntime(287): at android.app.Activity.startActivity(Activity.java:2923)
01-26 14:21:37.815: E/AndroidRuntime(287): ... 15 more
Android Manifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.Train"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".TrainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MenuActivity"
android:label="@string/app_name" />
</application>
</manifest>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为这是因为您的
MenuActivity
类与您的TrainActivity
类位于不同的包中。我不确定如何使用您拥有的包布局来做到这一点,但如果您有以下内容:那么您的清单条目将是:
I think its because your
MenuActivity
class is in a different package to yourTrainActivity
class. I'm not sure how to do it with the package layout you have, but if you had the following:Then your manifest entry would be:
我认为您尚未在清单文件中输入活动
每个活动必须存在于 AndroidManifest.xml 文件中
像这样
如果有问题然后使用
Intent myIntent = new Intent(TrainActivity.this, MenuActivity.class);
i think you have not entered activity in manifest file
each activity must be there in AndroidManifest.xml file
such like this
and if problem then use
Intent myIntent = new Intent(TrainActivity.this, MenuActivity.class);
您需要在 Activty 上实例化 ImageButtom:
祝您好运。
You need instanciation ImageButtom on Activty:
Good luck.
首先,您需要将活动添加到清单文件中(没有空格):
其次,对于按钮本身,这是有关如何完成此操作的建议:
First you need to add the activity to your manifest file (without whitespaces):
Second, for the button itself this is a suggestion for how it could be done:
确保将这两个活动添加到
AndroidManifest.xml
文件中。Make sure you add both activities in your
AndroidManifest.xml
file .我认为你有不同包中的课程。
I think you have classes in different packages.