Especially android-developers 🙂
I have some troubles with application menu which i try to create.
When I click on some button – apllication says that process stopped unexpectedly
I make it almost like in manual here(http://www.droidnova.com/creating-game-menus-in-android,518.html) but their sample works and my code not!
I don’t understand what i’ve done wrong, please, can somebody help?
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 for example
<?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>
Mani menu activity
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();
}
});
}
}
and an example StartGame activity
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);
}
}
and 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!
Well problem solved!
It was because of idk why but without full path for activities in 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>
Have you registered your classes in the Manifest? Every class that you want to use as an intent needs to be registered in the manifest, or your application won’t be able to find it.
Like this:
The action android:name is set so you can call it by name rather than calling it via Help.class.
That way in your code you’d be able to call it like this:
But that’s optional. Still, you have to have an entry in the manifest for the class for any of this to work.
Also it’s good practice to set your flags first and then set the content…
If none of that helps, post your stack trace so we can see what else might be the problem.