Android Custom Application global variables cannot be converted to type. android global variables

Source: Internet
Author: User

Android Custom Application global variables cannot be converted to type. android global variables
Today, I got a global variable AppContext, but the following error has always occurred. The original inherited Application must be declared in the configuration file.
Java. lang. runtimeException: Unable to start activity ComponentInfo {com. langteng. shiliao/com. langteng. shiliao. activity. mainActivity}: java. lang. classCastException: android. app. application cannot be cast to com. langteng. shiliao. until. appContext
Public class AppContext extends Application {
Public static final int NETTYPE_WIFI = 0x01;
Public static final int NETTYPE_CMWAP = 0x02;
Public static final int NETTYPE_CMNET = 0x03;
Public static final int PAGE_SIZE = 20; // default page size
Private static final int CACHE_TIME = 10*60000; // cache expiration time
.................................
Then, get an Application-type variable through getApplication () in the activity and convert it into a custom AppContext. As follows:
AppContext = (AppContext) getApplication ();
The result is a type conversion error during running.
After a long time, I found that the custom global Application must register the Declaration in AndroidManifest. xml:
<Application
Android: name = ". AppContext"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">

Global variables of android Application

No, you can declare it in the class instead of the onCreate method to use it in this Activity.
Public class GuessNumberActivity extends Activity {
Button btn1 = null;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Btn1 = (Button) findViewById (R. id. btn1 );
}
// Used in other functions
Bt1.setOnClickListener (new Button. onClickListener (){.........});

Android inherits the Application class and puts some global variables in it. How can I clear the value of the variable?

Is the variable cleared? Is it 0 or something?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.