问题
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