Osman Abdulle
Osman Abdulle

Reputation: 13

android app stops unexpectdly

I am trying to start another activity when a menu item is selected. However the app just stops unexpectedly when I run the application on my phone and press any of the menu items.

Here is an the NewPlanet activity I am trying to start for example:

package com.example.hello_world;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

public class NewPlanet extends Activity 
{
    @Override
    //onCreate method to start the app in system memory
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        //area to view the application content
        setContentView(R.layout.activity_add);
        //reference imageView UI element
        ImageView marsImage = (ImageView) findViewById(R.id.imageMars);
        //attach mars image to an event
        //onClick listener interface needs to be implemented
        marsImage.setOnClickListener(new View.OnClickListener()
        {

            @Override
            //onClick method for mars image being implemented
            public void onClick(View v) 
            {
                //create new WorldGen instance
                WorldGen mars = new WorldGen("Mars", 642, 3.7);
                //add a colony to the mars object
                mars.setPlanetColonies(1);
                //destroy the object after use
                finish();
            }

        });
    }

    //keyDown event handler
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        //check that if the x key has pressed down
        //any event handler can still operate on this event
        if (keyCode == KeyEvent.KEYCODE_X)
        {
            //return control from the function
            finish();
            return true;
        }

        return false;
    }
}

Here is the activity that contains menu event handler (onOptionsItemSelected) method I am starting the NewPlanet activity from:

package com.example.hello_world;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity 
{
    //new worldgen object
    WorldGen earth = new WorldGen("Earth", 5973, 9.78);

    @Override
    //parameter contains all the saved states and settings of the UI.
    protected void onCreate(Bundle savedInstanceState) 
    {
        //passes in the UI states to the onCreate initialzing method.
        super.onCreate(savedInstanceState);
        ////loads the XML layout definition via this method (specifies the directory).
        setContentView(R.layout.activity_main);

        //create new world object
        WorldGen earth = new WorldGen("Earth", 5973, 9.78);
        //add one colony to earth object
        earth.setPlanetColonies(1);
        //add a military base
        earth.setPlanetMilitary(1);
        //add 1000 immigrants to population
        earth.setColonyImmigration(1000);
        //add 100 soldiers
        earth.setBaseProtection(100);
        //turn the boolean force field on
        earth.turnForceFieldOn();

    }

    //set up world start values
    protected void setStartWorldValues()
    {

    }

    //set up the start screen text
    private void setUpScreenText() 
    {
        //textview object holding the actual values.
        //each textview object displays a different dataView
        //planet name textView.
        TextView planetNameValue = (TextView) findViewById (R.id.dataView1);
        planetNameValue.setText(earth.planetName);
        //planet mass textViewaq
        TextView planetMassValue = (TextView) findViewById (R.id.dataView2);
        planetMassValue.setText(earth.planetMass);
        //planet gravity textView
        TextView planetGravityValue = (TextView) findViewById (R.id.dataView3);
        planetGravityValue.setText(String.valueOf(earth.planetGravity));
        //planet colonies textView
        TextView viewplanetColoniesValue = (TextView) findViewById (R.id.dataView4);
        viewplanetColoniesValue.setText(String.valueOf(earth.planetColonies));
        //planet population textView
        TextView planetPopValue = (TextView) findViewById (R.id.dataView5);
        planetPopValue.setText(String.valueOf(earth.planetPopulation));
        //planet military textView
        TextView planetMilitaryValue = (TextView) findViewById (R.id.dataView6);
        planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
        //planet bases textView
        TextView planetBasesValue = (TextView) findViewById (R.id.dataView7);
        planetBasesValue.setText(String.valueOf(earth.planetBases));
        //planet forcefield state textView
        TextView planetFieldValue = (TextView) findViewById (R.id.dataView8);
        planetFieldValue.setText(String.valueOf(earth.getForceFieldState()));

    }

    @Override
    //inflates the menu with user defined xml menu file using the CreateOptionsMenu method
    //
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        //return value for a properly implemented menu resource
        return true;
    }

    @Override
    //decide what menu item has been selected 
    public boolean onOptionsItemSelected(MenuItem item) 
    {   
        //switch statement to determine which menu item was selected by ID
        switch (item.getItemId())
        {
            //menu add selected
            case R.id.menu_add:
                //explicit intent instance
                //call startActivity static method and start the class
                Intent intent_add = new Intent(this, NewPlanet.class);
                this.startActivity(intent_add);

                break;
            //config method selected
            case R.id.menu_config:
                //call startActivity static method and start the class
                Intent intent_config = new Intent(this, ConfigPlanet.class);
                this.startActivity(intent_config);
                break;
            case R.id.menu_travel:
                //call startActivity static method and start the class
                Intent intent_travel = new Intent(this, TravelPlanet.class);
                this.startActivity(intent_travel);
                break;
            case R.id.menu_attack:
                //call startActivity static method and start the class
                Intent intent_attack = new Intent(this, AttackPlanet.class);
                this.startActivity(intent_attack);
                break;
            default:
                //return control back to the superclass - like a return false
                return super.onOptionsItemSelected(item);
        }

        //return true for success
        return true;
    }

}

Here is my error log file (errors/warnings from a run just now):

eclipse.buildId=v22.3.0-887826
java.version=1.7.0_45
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_GB
Framework arguments:  -product com.android.ide.eclipse.adt.package.product
Command-line arguments:  -os win32 -ws win32 -arch x86_64 -product com.android.ide.eclipse.adt.package.product

Warning
Sat Dec 07 15:37:17 GMT 2013
Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\namso1902'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.

eclipse.buildId=v22.3.0-887826
java.version=1.7.0_45
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_GB
Framework arguments:  -product com.android.ide.eclipse.adt.package.product
Command-line arguments:  -os win32 -ws win32 -arch x86_64 -product com.android.ide.eclipse.adt.package.product

Warning
Sat Dec 07 15:37:17 GMT 2013
Warning: EGit couldn't detect the installation path "gitPrefix" of native Git. Hence EGit can't respect system level
Git settings which might be configured in ${gitPrefix}/etc/gitconfig under the native Git installation directory.
The most important of these settings is core.autocrlf. Git for Windows by default sets this parameter to true in
this system level configuration. The Git installation location can be configured on the
Team > Git > Configuration preference page's 'System Settings' tab.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.

Here is the xml 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="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.hello_world.MainActivity"
            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="com.example.hello_world.NewPlanet"/>
        <activity android:name="com.example.hello_world.ConfigPlanet"/>
        <activity android:name="com.example.hello_world.TravelPlanet"/>
        <activity android:name="com.example.hello_world.AttackPlanet"/>
    </application>

</manifest>

Cannot get the stack trace sorry.

Thanks in advance

Upvotes: 1

Views: 244

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

Your package name is

 package="com.example.hello_world"  // in manifest

But you have

 <activity android:name="chapter.two.hello_World.NewPlanet"/> 
 <activity android:name="chapter.two.hello_World.ConfigPlanet"/>
 <activity android:name="chapter.two.hello_World.TravelPlanet"/>
 <activity android:name="chapter.two.hello_World.AttackPlanet"/>

Should be

 <activity android:name="com.example.hello_world.NewPlanet"/>
 //similar for other activities if it is in package com.example.hello_world;

coz your package name is

 package com.example.hello_world; // for NewPlanet

Upvotes: 1

Related Questions