【问题标题】:Android's Menu TroublesAndroid的菜单问题
【发布时间】:2011-11-05 21:41:15
【问题描述】:

尤其是安卓开发者 :) 我尝试创建的应用程序菜单有一些问题。

当我点击某个按钮时 - 应用程序说 进程意外停止

我在这里(http://www.droidnova.com/creating-game-menus-in-android,518.html)几乎像手册中一样,但他们的示例有效,而我的代码无效! 我不明白我做错了什么,请有人帮忙吗?

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TableLayout
android:id="@+id/widget28"
android:layout_width="295px"
android:layout_height="600px"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
<Button
android:id="@+id/StartGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/startgame_button"
>
</Button>
<TextView
android:id="@+id/widget34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</TextView>
<Button
android:id="@+id/Help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/help_button" 
>
</Button>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</TextView>
<Button
android:id="@+id/Highscores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/highscores_button" 
>
</Button>
<TextView
android:id="@+id/widget36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</TextView>
<Button android:id="@+id/Quit" android:background="@drawable/quit_button" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>

</TableLayout>
</RelativeLayout>

例如 startgame_button.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/start_game_highlighted" /> 
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/start_game_pressed" /> 
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/start_game_pressed" /> 
    <item android:drawable="@drawable/start_game" /> 
</selector>

菜单活动

package game.mainmenu;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import game.main.R;

public class MainMenuActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Activity t = this;
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN );

        Button StartGameButton = (Button)findViewById(R.id.StartGame);//кнопка начала игры
        StartGameButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent StartGameIntent = new Intent(MainMenuActivity.this, StartGame.class);
                startActivity(StartGameIntent);
            }
        });

        Button HelpButton = (Button)findViewById(R.id.Help);//кнопка help
        HelpButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent HelpIntent = new Intent(MainMenuActivity.this, Help.class);
                startActivity(HelpIntent);
            }
        });

        Button OptionsButton = (Button)findViewById(R.id.Highscores);//кнопка 
        OptionsButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent OptionsIntent = new Intent(MainMenuActivity.this, Highscores.class);
                startActivity(OptionsIntent);
            }
        });

        Button QuitButton = (Button)findViewById(R.id.Quit);//кнопка 
        QuitButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                t.finish();
            }
        });


    }


}

还有一个 StartGame 活动示例

public class StartGame extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.copy);      

    }
}

和copy.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<TextView android:text="blah blah blah" 
android:id="@+id/TextView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></TextView>
</RelativeLayout>

logcat 跟踪

11-05 22:47:53.166: INFO/ActivityManager(67): Displayed game.main/game.mainmenu.MainMenuActivity: +15s468ms
11-05 22:47:59.306: DEBUG/dalvikvm(319): GC_EXPLICIT freed 2K, 54% free 2539K/5511K, external 1625K/2137K, paused 1033ms
11-05 22:48:05.456: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol
11-05 22:48:06.117: DEBUG/dalvikvm(150): GC_EXPLICIT freed 100K, 49% free 3039K/5895K, external 6009K/7443K, paused 437ms
11-05 22:48:06.997: INFO/ActivityManager(67): Starting: Intent { act=lolhelp } from pid 851
11-05 22:48:07.036: DEBUG/AndroidRuntime(851): Shutting down VM
11-05 22:48:07.036: WARN/dalvikvm(851): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-05 22:48:07.137: ERROR/AndroidRuntime(851): FATAL EXCEPTION: main
11-05 22:48:07.137: ERROR/AndroidRuntime(851): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=lolhelp }
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at android.app.Activity.startActivityForResult(Activity.java:2827)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at android.app.Activity.startActivity(Activity.java:2933)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at game.mainmenu.MainMenuActivity$2.onClick(MainMenuActivity.java:40)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at android.view.View.performClick(View.java:2485)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at android.view.View$PerformClick.run(View.java:9080)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at android.os.Handler.handleCallback(Handler.java:587)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at android.os.Looper.loop(Looper.java:123)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at java.lang.reflect.Method.invoke(Method.java:507)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-05 22:48:07.137: ERROR/AndroidRuntime(851):     at dalvik.system.NativeStart.main(Native Method)
11-05 22:48:07.227: WARN/ActivityManager(67):   Force finishing activity game.main/game.mainmenu.MainMenuActivity
11-05 22:48:07.847: WARN/ActivityManager(67): Activity pause timeout for HistoryRecord{4065f5c0 game.main/game.mainmenu.MainMenuActivity}
11-05 22:48:22.997: WARN/ActivityManager(67): Activity destroy timeout for HistoryRecord{4065f5c0 game.main/game.mainmenu.MainMenuActivity}

更新!

问题解决了! 这是因为 idk 为什么但在 AndroidManifest 中没有活动的完整路径

<activity android:name="game.mainmenu.Help"//like here. it dosen't work if it's just ".Help"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

【问题讨论】:

  • 请使用您获得的 logcat 堆栈跟踪编辑您的问题
  • 完成!抱歉让你久等了——现在才凌晨 3 点,我有点困了

标签: java android xml button menu


【解决方案1】:

您是否在 Manifest 中注册了您的课程?您想要用作意图的每个类都需要在清单中注册,否则您的应用程序将无法找到它。

像这样:

 <activity android:name="Help" android:label="@string/app_name"  

        >
        <intent-filter>
            <action android:name="this.can.be.anything.unique" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

操作 android:name 已设置,因此您可以按名称调用它,而不是通过 Help.class 调用它。

在你的代码中你可以这样调用它:

Intent helpIntent = new Intent("this.can.be.anything.unique");
startActivity(helpIntent);

但这是可选的。不过,您必须在类的清单中有一个条目才能使任何这些工作。

另外,最好先设置标志,然后设置内容...

    final Activity t = this;
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN );
    setContentView(R.layout.main);//move this here

如果这些都没有帮助,请发布您的堆栈跟踪,以便我们查看可能还有什么问题。

【讨论】:

  • 哦,我写了&lt;activity android:name=".Help"&gt;&lt;/activity&gt;之类的东西
猜你喜欢
  • 1970-01-01
  • 2012-07-22
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多