Amjad Mesmar
Amjad Mesmar

Reputation: 86

App keep crashing after trying to create new activity?

I wanted to Create A Second Activity to my App and then the App gives black screen and crashes every time it switches to the new Activity.

The app works perfectly when disable the new activity. I tried a lot to fix it but no use. I maybe miss something .

Please Notice I Made the 2nd activity LAUNCHER category and changed the first to DEFAULT and the Emulator don't show the app when running it said that it's installed but it wont run and it's not on the Virtual Mobile Menu.

Here is the 2nd Activity xml File :

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MenuActivity" >

<TextView
    android:id="@+id/WelcomeText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="41dp"
    android:text="Welcome !"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/Play"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/WelcomeText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="48dp"
    android:text="S t a r t   G a m e" />

<Button
    android:id="@+id/QuitMenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="41dp"
    android:text="Quit Game" />

    </RelativeLayout>

Here it the Java Code of it :

    package com.example.write_example;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;

    public class MenuActivity extends Activity {

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

    Button start = (Button) findViewById(R.id.Play);
    Button exit = (Button) findViewById(R.id.QuitMenu);

    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
   startActivity (newIntent("android.intent.action.MAIN"));             
        }
    });

       exit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
        }
    });

}



  }

And Here is The Manifest Code :

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

<uses-sdk
    android:minSdkVersion="1"
    android:targetSdkVersion="17" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="MenuActivity"
        android:label="@string/app_name" android:launchMode="standard">
        <intent-filter>
            <action android:name="android.intent.action.MainMenu" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Any Help Is Appreciated , Thanks for your time .

Upvotes: 0

Views: 1027

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

<Button
    android:id="@+id/QuitMenu"

So change

Button exit = (Button) findViewById(R.id.QuitGame);// id is QuitMenu

to

Button exit = (Button) findViewById(R.id.QuitMenu);

Edit:

<activity
    android:name=".MenuActivity"
    android:launchMode="standard"
    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=".MainActivity"
    android:label="@string/app_name">

</activity>

And Change

startActivity (newIntent("android.intent.action.MAIN")); 

To

startActivity (new Intent(MenuActivity.this,MainActivity.class)); 

Upvotes: 2

Vimal Rughani
Vimal Rughani

Reputation: 234

QuitGame is text property of button not id. Id is QuitMenu.

Upvotes: 1

Related Questions