Android self-defined application global variable cannot type conversion problem

Source: Internet
Author: User

Today, we have a global variable appcontext, but it always appears, such as the following error, the original inheritance application in the manifest file declaration.
Java.lang.RuntimeException:Unable to start Activity componentinfo{com.langteng.shiliao/ Com.langteng.shiliao.activity.MainActivity}: Java.lang.ClassCastException:android.app.Application cannot is 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 paging size
private static final int cache_time = 10*60000;//Cache Expiration Time
.................................
It then obtains a variable of type application by Getapplication () in the activity and transforms it into its own defined appcontext. For example, the following:
AppContext = (appContext) getapplication ();
The result is that the type conversion error is reported at execution time.
Later on the internet for a long time to find their own definition of the global application must be in the Androidmanifest.xml Register statement:
<application
Android:name= ". AppContext "
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >

Android self-defined application global variable cannot type conversion problem

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.