Android的菜单故障

时间:2011-11-05 21:41:15

标签: java android xml button menu

特别是android-developers :) 我尝试创建的应用程序菜单有些麻烦。

当我点击某个按钮时 - apllication表示进程意外停止

我几乎就像在这里的手册(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 track

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}

UPD!

问题解决了! 这是因为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>

1 个答案:

答案 0 :(得分:0)

您是否在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

如果没有任何帮助,请发布您的堆栈跟踪,以便我们可以看到还有什么问题。