简体   繁体   English

带有多个应用程序标签的AndroidManifest.xml

[英]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. 我对Android编程非常陌生,而且我一直试图找出为什么我的应用程序在按下按钮时强行关闭。 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? 是否可以在清单xml中包含多个<application>标记?

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. MyApp应用程序只是一个“全局变量”的应用程序,因为我想没有新的应用程序就没有办法做到这一点。

Here is the code for MyApp in case it helps: 以下是MyApp的代码,以防它有用:

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 . 只需要<manifest>和<application>元素, 它们必须存在且只能出现一次

What I think you want is to use your custom Application as the main Application . 我认为你想要的是使用自定义Application作为主要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> ,只需将其名称指定给主<application> (您需要指定其完整包)。

<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. 只需要'manifest'和'application'元素,它们必须存在并且只能出现一次。 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. 大多数其他人可以多次出现或者根本不出现 - 尽管至少其中一些必须出现以便清单完成任何有意义的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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