Android 应用程序在按钮单击时崩溃

发布于 2024-12-29 01:02:27 字数 5978 浏览 0 评论 0原文

您好,我编写了一个带有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

站稳脚跟 2025-01-05 01:02:27

我认为这是因为您的 MenuActivity 类与您的 TrainActivity 类位于不同的包中。我不确定如何使用您拥有的包布局来做到这一点,但如果您有以下内容:

com.ardroid.train.TrainActivity
com.ardroid.train.menu.MenuActivity

那么您的清单条目将是:

<activity 
    android:name=".menu.MenuActivity "
    android:label="@string/app_name" />

I think its because your MenuActivity class is in a different package to your TrainActivity class. I'm not sure how to do it with the package layout you have, but if you had the following:

com.ardroid.train.TrainActivity
com.ardroid.train.menu.MenuActivity

Then your manifest entry would be:

<activity 
    android:name=".menu.MenuActivity "
    android:label="@string/app_name" />
×眷恋的温暖 2025-01-05 01:02:27

我认为您尚未在清单文件中输入活动
每个活动必须存在于 AndroidManifest.xml 文件中
像这样

   <application  
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity 
        android:name=".TrainActivity "
        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=".MenuActivity "
        android:label="@string/app_name" />
</application>

如果有问题然后使用
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

   <application  
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity 
        android:name=".TrainActivity "
        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=".MenuActivity "
        android:label="@string/app_name" />
</application>

and if problem then use
Intent myIntent = new Intent(TrainActivity.this, MenuActivity.class);

暗喜 2025-01-05 01:02:27

您需要在 Activty 上实例化 ImageButtom:

ImageButton btnImg;

... oncreadte(...) {

btnImg = (ImageButton) findViewById(R.id.enterButton);

...

btnImg.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

// This call new Activity on press ImageButton
Intent it = new Intent(TrainActivity.this, MenuActivity.class);
startActivity(it);

}

}

祝您好运。

You need instanciation ImageButtom on Activty:

ImageButton btnImg;

... oncreadte(...) {

btnImg = (ImageButton) findViewById(R.id.enterButton);

...

btnImg.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

// This call new Activity on press ImageButton
Intent it = new Intent(TrainActivity.this, MenuActivity.class);
startActivity(it);

}

}

Good luck.

萌无敌 2025-01-05 01:02:27

首先,您需要将活动添加到清单文件中(没有空格):

<activity android:name=".MenuActivity"></activity>

其次,对于按钮本身,这是有关如何完成此操作的建议:

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 
    {

    ImageButton button;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (ImageButton) findViewById(R.id.enterButton);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                   goMenu();
            }

        });
    }


    public void goMenu()
    {
      Intent myIntent = new Intent(this, MenuActivity.class);
      startActivity(myIntent);
    }    
}

First you need to add the activity to your manifest file (without whitespaces):

<activity android:name=".MenuActivity"></activity>

Second, for the button itself this is a suggestion for how it could be done:

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 
    {

    ImageButton button;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (ImageButton) findViewById(R.id.enterButton);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                   goMenu();
            }

        });
    }


    public void goMenu()
    {
      Intent myIntent = new Intent(this, MenuActivity.class);
      startActivity(myIntent);
    }    
}
夏九 2025-01-05 01:02:27

确保将这两个活动添加到 AndroidManifest.xml 文件中。

Make sure you add both activities in your AndroidManifest.xml file .

紫﹏色ふ单纯 2025-01-05 01:02:27

我认为你有不同包中的课程。

I think you have classes in different packages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文