MyApplication not assignable to Activity

流过昼夜 提交于 2019-12-13 07:04:07

问题


I am currently attempting to make a global variable activity.

I have followed the following instructions (Android global variable) in order to set the Activity up.

However, the problem comes when I attempt to edit the android:name attribute. When I put in the name of the application/activity, the error message says that I cannot extend Application. Can someone explain why?

Manifest:

<?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>

And Protoype2 activity:

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;
    }

}

回答1:


Application and Activity are two separate classes. If you're extending the Application class then don't declare the same class as an Activity also in the manifest -

Remove this code from manifest -

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



回答2:


the xml is dummy, and more like a information holder/skeleton for the app either layouts or manifest, they can not use logic, intance objects or use getters/setter.




回答3:


The "Protoype2" is the Application class. And you can't declare the Application class as activity. You need to have an Activity class.

The link you posted is quite str8 forward how to access the Application class from an activity.




回答4:


<?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>

Replace With

<?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>


来源:https://stackoverflow.com/questions/36100973/myapplication-not-assignable-to-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!