7

I'm very new to Android programming and I've been trying to figure out why my app is force-closing on a button-click. I've narrowed it down to a few things.

One question; Is it possible to have more than one <application> tag in the manifest xml?

Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.dummies.android.beergoggles"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".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="Result" android:label="@string/app_name">        </activity>
</application>
<application android:name="MyApp" 
   android:icon="@drawable/icon" 
   android:label="@string/app_name2"></application>

I've been researching, but found only a vague post about creating a new manifest file for a new application. The MyApp application is just an app for a "global variable", since I guess there's no way to do that without a new application.

Here is the code for MyApp in case it helps:

import android.app.Application;

public class MyApp extends Application{

public static int resultCount;

public int getCount(){
    return resultCount;
  }
public void setCount(int c){
    resultCount = c;
}
}

Any help would be much appreciated.

Sepster
  • 4,800
  • 20
  • 38
Nicknack1016
  • 95
  • 1
  • 2
  • 5

3 Answers3

20

According to documentation manifest file with only one application element is valid.

Only the <manifest> and <application> elements are required, they each must be present and can occur only once.

Sepster
  • 4,800
  • 20
  • 38
Marcin Pietraszek
  • 3,134
  • 1
  • 19
  • 31
10

What I think you want is to use your custom Application as the main Application.

So you dont add a new <application> but just specify its name to the main <application>(you need to specify its full package).

<application android:icon="@drawable/icon" android:label="@string/app_name" android:name:"com.mypackage.MyApp"> <!-- Added the android:name -->
    <activity android:name=".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="Result" android:label="@string/app_name">        </activity>
</application>

See info here

ccheneson
  • 49,072
  • 8
  • 63
  • 68
0

Only the 'manifest' and 'application' elements are required, they each must be present and can occur only once. Most of the others can occur many times or not at all — although at least some of them must be present for the manifest to accomplish anything meaningful.

Niket K
  • 107
  • 1
  • 7