我创建了一个GlobalVariables类来跨多个类共享变量。但我现在遇到classcastexception的问题。我一直在阅读有关此异常的内容,我知道我无法将主类的实例强制转换为子类。仍然不确定如何在我的代码中更改它以摆脱它。请快速查看我的TextPlay class
GlobalVariables gs = (GlobalVariables) getApplication();
gs.setTestMe(getUserId);
tvUserId.setText("Copied");
是。非常感谢!
Globalvariables类
public class GlobalVariables extends Application {
private String testMe;
public String getTestMe() {
return testMe;
}
public void setTestMe(String testMe) {
this.testMe = testMe;
}
}
TextPlay类
public class TextPlay extends Activity implements View.OnClickListener {
Button chkCmd;
ToggleButton passTog;
EditText input;
TextView display;
EditText etUserId;
Button btnUserId;
String getUserId;
TextView tvUserId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
baconAndEggs();
passTog.setOnClickListener(this);
chkCmd.setOnClickListener(this);
// Share variables between classes controls
etUserId = (EditText) findViewById(R.id.etUserId);
btnUserId = (Button) findViewById(R.id.btnUserId);
tvUserId = (TextView) findViewById(R.id.tvUserId);
btnUserId.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getUserId = etUserId.getText().toString();
// Store Global Variables in GlobalVariables class
GlobalVariables gs = (GlobalVariables) getApplication();
gs.setTestMe(getUserId);
tvUserId.setText("Copied");
}
});
}
private void baconAndEggs() {
// TODO Auto-generated method stub
chkCmd = (Button) findViewById(R.id.bResults);
passTog = (ToggleButton) findViewById(R.id.tbPassword);
input = (EditText) findViewById(R.id.etCommands);
display = (TextView) findViewById(R.id.tvResults);
}
public void onClick(View v) {
}
}
}
错误日志
01-22 06:12:23.898: E/AndroidRuntime(2605): FATAL EXCEPTION: main
01-22 06:12:23.898: E/AndroidRuntime(2605): java.lang.ClassCastException: android.app.Application cannot be cast to com.armstrong.y.android.app.GlobalVariables
01-22 06:12:23.898: E/AndroidRuntime(2605): at com.armstrong.y.android.app.TextPlay$1.onClick(TextPlay.java:48)
01-22 06:12:23.898: E/AndroidRuntime(2605): at android.view.View.performClick(View.java:4202)
01-22 06:12:23.898: E/AndroidRuntime(2605): at android.view.View$PerformClick.run(View.java:17340)
01-22 06:12:23.898: E/AndroidRuntime(2605): at android.os.Handler.handleCallback(Handler.java:725)
01-22 06:12:23.898: E/AndroidRuntime(2605): at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 06:12:23.898: E/AndroidRuntime(2605): at android.os.Looper.loop(Looper.java:137)
01-22 06:12:23.898: E/AndroidRuntime(2605): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-22 06:12:23.898: E/AndroidRuntime(2605): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 06:12:23.898: E/AndroidRuntime(2605): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 06:12:23.898: E/AndroidRuntime(2605): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-22 06:12:23.898: E/AndroidRuntime(2605): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-22 06:12:23.898: E/AndroidRuntime(2605): at dalvik.system.NativeStart.main(Native Method)
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.armstrong.y.android.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.armstrong.y.android.app.NumCounter"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="com.armstrong.y.android.app.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="com.armstrong.y.android.app.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Email"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".DailyDataEntry"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".WeeklyDataEntry"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Camera"
android:label="@string/title_activity_main"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".InitialDataEntry"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".FinalDataEntry"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Rankings"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".Data"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".OpenedClass"
android:label="@string/title_activity_main" >
</activity>
<activity
android:name=".GlobalVariables"
android:label="@string/title_activity_main" >
</activity>
</application>
</manifest>
答案 0 :(得分:1)
尝试使用此代码初始化扩展Application
GlobalVariables gs = (GlobalVariables) ActivityName.this.getApplicationContext();
更新的答案: 在manifest.xml中,
<{1>}代码中的,请使用application
不要将其声明为android:name= "<your_package_name>.GlobalVariables"
。
答案 1 :(得分:0)
您的Application
对象看起来不是GlobalVariables
。我希望您没有声明GlobalVariables
是清单中的应用程序对象。你需要为类转换异常做到这一点才能消失。 (将android:name=".GlobalVariables"
之类的内容添加为<application>
标记的属性。)
但是!不要通过扩展Application
来实现全局变量。这不是必需的;只需创建单例类或具有静态数据的类。如有必要,维护一个“初始化”标志,可以在需要时检查并用于执行初始化。
来自the docs:
通常不需要子类Application。在大多数情况下,静态单例可以以更模块化的方式提供相同的功能。如果你的单例需要一个全局上下文(例如注册广播接收器),那么检索它的函数可以给一个Context,它在第一次构造单例时在内部使用Context.getApplicationContext()。