简体   繁体   中英

AndroidManifest.xml with multiple application tags

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.

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 .

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

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.

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