简体   繁体   中英

Android activity crashes on button click

When I click the button to go to the next activity, my app crashes. I don't know if it's an issue with my Game class, Menu class, the game layout or the Manifest. I assume it's the manifest though.

I've tried tons of different ways to solve the problem. I changed the name of the intent in the manifest, I changed the way I call the activity in the java class...but nothing worked.

Can someone please help?

I think there's an issue with the way I'm declaring the activities in the manifest but I am not sure.

Here is the code:

Menu.java:

package com.example.hello_world;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Menu extends Activity implements OnClickListener {

Button clickToStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_activity);
    clickToStart = (Button) findViewById(R.id.click);
    clickToStart.setOnClickListener(this);

}


@Override
public void onClick(View v) {
    Intent openGame = new Intent(getApplicationContext(), Game.class);
    startActivity(openGame);

}

}

Game.java:

package com.example.hello_world;
import android.app.Activity;
import android.os.Bundle;

public class Game extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_activity);
}   
}

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello_world"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".Menu"
        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=".Game"
        android:label="@string/app_name" 
        android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="com.example.hello_world.GAME" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>

game layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Game" >

<Button
    android:id="@+id/button1"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="18dp"
    android:text="a" />

<Button
    android:id="@+id/button2"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_toRightOf="@+id/button1"
    android:text="b" />

<Button
    android:id="@+id/button4"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button3"
    android:layout_alignBottom="@+id/button3"
    android:layout_toRightOf="@+id/button3"
    android:text="c" />

<Button
    android:id="@+id/button5"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button4"
    android:layout_alignBottom="@+id/button4"
    android:layout_toRightOf="@+id/button4"
    android:text="d" />

<Button
    android:id="@+id/button6"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/button1"
    android:layout_below="@+id/button1"
    android:text="e" />

<Button
    android:id="@+id/button7"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button6"
    android:layout_alignBottom="@+id/button6"
    android:layout_alignRight="@+id/button2"
    android:layout_toRightOf="@+id/button6"
    android:text="f" />

<Button
    android:id="@+id/button8"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_below="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:text="g" />

<Button
    android:id="@+id/button3"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_above="@+id/button7"
    android:layout_toRightOf="@+id/button2"
    android:text="h" />

<Button
    android:id="@+id/button9"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button8"
    android:layout_alignBottom="@+id/button8"
    android:layout_toLeftOf="@+id/button5"
    android:text="i" />

<Button
    android:id="@+id/button10"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_below="@+id/button4"
    android:layout_toRightOf="@+id/button4"
    android:text="j" />

<Button
    android:id="@+id/button11"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/button6"
    android:layout_below="@+id/button6"
    android:text="k" />

<Button
    android:id="@+id/button13"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignLeft="@+id/button8"
    android:layout_below="@+id/button8"
    android:text="l" />

<Button
    android:id="@+id/button14"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button12"
    android:layout_alignBottom="@+id/button12"
    android:layout_toRightOf="@+id/button13"
    android:text="m" />

<Button
    android:id="@+id/button16"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/button21"
    android:layout_below="@+id/button12"
    android:text="n" />

<Button
    android:id="@+id/button17"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_below="@+id/button12"
    android:layout_toLeftOf="@+id/button18"
    android:layout_toRightOf="@+id/button16"
    android:text="o" />

<Button
    android:id="@+id/button19"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button18"
    android:layout_alignBottom="@+id/button18"
    android:layout_toLeftOf="@+id/button15"
    android:text="p" />

<Button
    android:id="@+id/button20"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button17"
    android:layout_alignBottom="@+id/button17"
    android:layout_alignLeft="@+id/button10"
    android:text="q" />

<Button
    android:id="@+id/button18"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_below="@+id/button14"
    android:layout_toLeftOf="@+id/button14"
    android:text="r" />

<Button
    android:id="@+id/button15"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignLeft="@+id/button10"
    android:layout_below="@+id/button10"
    android:text="t" />

<Button
    android:id="@+id/button21"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/button6"
    android:layout_below="@+id/button16"
    android:text="u" />

<Button
    android:id="@+id/button22"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button21"
    android:layout_alignBottom="@+id/button21"
    android:layout_alignLeft="@+id/button17"
    android:layout_alignRight="@+id/button17"
    android:text="v" />

<Button
    android:id="@+id/button23"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button22"
    android:layout_alignBottom="@+id/button22"
    android:layout_alignLeft="@+id/button18"
    android:text="w" />

<Button
    android:id="@+id/button24"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button23"
    android:layout_alignBottom="@+id/button23"
    android:layout_toRightOf="@+id/button23"
    android:text="x" />

<Button
    android:id="@+id/button25"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignBaseline="@+id/button24"
    android:layout_alignBottom="@+id/button24"
    android:layout_alignLeft="@+id/button20"
    android:text="y" />

<Button
    android:id="@+id/button12"
    android:layout_width="@string/size"
    android:layout_height="@string/size"
    android:layout_alignRight="@+id/button7"
    android:layout_below="@+id/button6"
    android:layout_toRightOf="@+id/button6"
    android:text="s" />

Error Log:

在此处输入图片说明

You currently have all the layout dimensions for your buttons set to String references. Layout dimensions need to be integers - you can store these in dimens.xml like so:

<dimen name="size">16dp</dimen> <!-- change size to what you need -->

Then, use layout_width="@dimen/size" for your views (and layout_height as well).

You should be using the Activity context to launch an Activity instead of Application Context.

Since Activity extends Context, you can use the this keyword to reference context.

@Override
public void onClick(View v) {
    Intent openGame = new Intent(this, Game.class);
    startActivity(openGame);

}

If you really want to use application context (which I do not think you meant to do), then I believe you would have to start the Activity in a different way, in a new task.

Remove the intent filter for Game Activity and make sure the class is located in the right package

    <intent-filter>
        <action android:name="com.example.hello_world.GAME" />
        <category android:name="android.intent.category.DEFAULT" />

And this is not the right way to set the height and width of buttons inside the game layout: Correct them.

<Button
android:id="@+id/button2"
android:layout_width="@string/size"
android:layout_height="@string/size"
android:text="b" />

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM