MyApplication无法分配给Activity

时间:2016-03-19 11:12:30

标签: java android android-manifest extending-classes

我目前正在尝试进行全局变量活动。

我已遵循以下说明(Android global variable)以设置活动。

然而,当我尝试编辑 android:name 属性时出现问题。当我输入应用程序/活动的名称时,错误消息表明我无法扩展应用程序。有人可以解释原因吗?

清单:

package com.example.denny.protoype2;

import android.app.Application;


public class Protoype2 extends Application {
    private boolean StopTrue;

    public boolean getStopTrue() {
        return StopTrue;
    }

    public void setStopTrue (boolean StopTrue) {
        this.StopTrue = StopTrue;
    }

}

和Protoype2活动:

save!

4 个答案:

答案 0 :(得分:1)

xml是虚拟的,更像是应用程序的布局或清单的信息持有者/骨架,它们不能使用逻辑,intance对象或使用getter / setter。

答案 1 :(得分:1)

Application和Activity是两个独立的类。如果您正在扩展Application类,那么也不要在清单中声明与Activity相同的类 -

从清单中删除此代码 -

<activity
    android:name=".Protoype2"
    android:label="@string/title_activity_global_var"
    android:theme="@style/AppTheme.NoActionBar">
</activity>

答案 2 :(得分:1)

&#34; Protoype2&#34;是Application类。而且您不能将Application类声明为活动。你需要一个Activity类。

您发布的链接非常明确如何从活动中访问Application类。

答案 3 :(得分:1)

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:name="Protoype2"
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
    android:name=".Protoype2"
    android:label="@string/title_activity_global_var"
    android:theme="@style/AppTheme.NoActionBar">
</activity>
</application>

替换为

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:name=".Protoype2"
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

</application>