我在下面的代码行中遇到了类强制转换异常。我已经在下面附加了堆栈跟踪,下面的行是339,如堆栈跟踪中所述。
com.unique.may.utility.GlobalStateData gs = (com.unique.may.utility.GlobalStateData)
getApplication();
在全局状态数据中,代码是
public class GlobalStateData extends Application {
private String findUsBranchEMailId;
public void setFindUsBranchEMailId(String findUsBranchEMailId) {
this.findUsBranchEMailId = findUsBranchEMailId;
}
public String getFindUsBranchEMailId() {
return findUsBranchEMailId;
}
}
堆栈跟踪
java.lang.ClassCastException: android.app.Application
at com.style.IntlLayout.Find.onChildClick(Find.java:339)
at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:588)
at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:527)
下面是我的清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.styling.Layout"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_DEBUG_APP"></uses-permission>
<support-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>
<uses-library android:name="com.google.android.maps" />
<application android:icon="@drawable/launcher"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".Catalog" android:configChanges="orientation|keyboardHidden"> </activity>
<activity android:name=".Feedback" android:configChanges="orientation|keyboardHidden"></activity>
<activity
android:label="@string/app_name"
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
期待您的回复。 感谢。
答案 0 :(得分:2)
为了使用getApplication()函数,您必须先在Manifest中注册,Android会自动为您实例化。
<application android:name=".ApplicationName"
答案 1 :(得分:1)
答案 2 :(得分:1)
您可以访问下面的findUsBranchEMailId
,
((GlobalStateData)getApplication()).setFindUsBranchEMailId("something");
获取它,
((GlobalStateData)getApplication()).getFindUsBranchEMailId());
另外,将您的Application类名添加到Manifest文件中的Application Tag,
<application android:icon="@drawable/launcher" android:label="@string/app_name"
android:debuggable="true" android:name=".GlobalStateData">
答案 3 :(得分:1)
好的,你做错了,
创建类似于
的应用程序类class MyApp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
}
在活动中获取您的Application类对象,
class Blah extends Activity {
@Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
在清单文件中声明您的Application类,如
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".MyApp">
有关详情,请参阅本教程Android Application Class