首页 > 编程语言 >解决android java.lang.ClassCastException android.app.Application

解决android java.lang.ClassCastException android.app.Application

时间:2022-11-14 16:32:04浏览次数:43  
标签:lang java String your application GlobalState android class


定义类 DemoApp , 结果 Activity 调用始终报类错郁闷呀!


class DemoApp extends Application{

}


   下面的配置注意:


   


<application android:icon="@drawable/icon"

android:label="@string/app_name"

android:debuggable="true"

android:name=".DemoApp">





android:debuggable="true" 允许调试的意思











Firstly, you should create a new class that will behave as our global object, for example :


package           com.jameselsey.domain;        

import java.util.ArrayList;
import java.util.List;
import com.google.android.maps.GeoPoint;
import android.app.Application;

/**
* This is a global POJO that we attach data to which we
* want to use across the application
* @author James Elsey
*
*/
public class GlobalState extends Application
{
private String testMe;

public String getTestMe() {
return testMe;
}
public void setTestMe(String testMe) {
this .testMe = testMe;
}
}



The above class declares one String, with accessor methods. Soon we will see how this object will be available throughout the application. You can add anything you want onto this object, in my application I have a List of other domain classes that I wanted to use elsewhere in my application.

Right, now that you have your global class defined, you need to specify this in your AndroidManifest file, otherwise the application won’t know where to find this class, and you will get a ClassNotFoundException

Locate your application tag, and add this into it :


android:name=         "com.jameselsey.domain.GlobalState"



It must reference your fully qualified global state class

Now you have it set, your ready to start putting data in, and pulling data out, use the following


// Set values        
GlobalState gs = (GlobalState) getApplication();
gs.setTestMe( "Some String" );</code>

// Get values
GlobalState gs = (GlobalState) getApplication();
String s = gs.getTestMe();



And thats it! Off you go!



标签:lang,java,String,your,application,GlobalState,android,class
From: https://blog.51cto.com/jdsjlzx/5849486

相关文章

  • Android Service学习之AIDL, Parcelable和远程服务
    AIDL的作用由于每个应用程序都运行在自己的进程空间,并且可以从应用程序UI运行另一个服务进程,而且经常会在不同的进程间传递对象。在Android平台,一个进程通常不能访问另......
  • java 通用 post 请求
    java 实现 Http的 Post 请求 用 Json 为参数调用://_http_url_cdr为//"http://10.10.10.243:15642/call/upload/ctiCdrUpload"//eventJson为//{"callid":"1......
  • Java + POI导出富文本的内容到word文档
    一、需求:当创建使用富文本编辑器,操作完的数据,传输到后台都是带有html标签的。如:<h1>标题头</h1><h2>第二个标题</h2><ahref="www.baidu.com">百度搜索</a>我们想把富文......
  • Java json 字符串转化 JSONObject
    json字符串转化JSONObjectStrings="{\"msg\":\"操作成功\",\"code\":200,\"data\":{\"bizTypeList\":[{\"bizname\":\"测试一\"},{\"bizname\":\"测试二\"},{\&q......
  • supervisor管理java进程
    安装yuminstallsupervisor设置开机启动systemctlenablesupervisord启动supervisordsystemctlstartsupervisord配置java进程[program:monitorapi]command=......
  • 关于poi取消合并区域的方法-java
    //主要用于原来的excel模板已经存在合并区域、再次合并会导致合并异常privatebooleanremoveMergedRegion(Sheetsheet,CellRangeAddressmergedRegionToRemove){......
  • Java中通过反射+自定义注解判断对象中部分属性是否为空,返回为空字段的名称或自定义含
    场景若依管理系统前后端分离版基于ElementUI和SpringBoot怎样实现Excel导入和导出:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108278834在上面进行exc......
  • java基础笔记
    java的数据类型分为两大类  进制前缀二进制:0b八进制:0十六进制:0xJava会直接将它们转换为十进制输出 float、double并不能准确表示每一位小数,对于有的小数只能无......
  • 《JavaSE-第十九章》之Collection
    《JavaSE-第十九章》之Collection前言在你立足处深挖下去,就会有泉水涌出!别管蒙昧者们叫嚷:“下边永远是地狱!”博客主页:​​KC老衲爱尼姑的博客主页​​​​博主的github,......
  • Android自定义定时闹钟开发详解
    这篇文章主要为大家详细介绍了Android自定义定时闹钟开发,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下本文实例为大家分享了Android开发之自......