我创建了一个 GlobalVariables 类来跨多个类共享变量。但是我现在遇到了 classcastexception 的问题。我一直在阅读有关此异常的信息,并且我知道我无法将主类的实例强制转换为子类。仍然不确定如何在我的代码中更改它以摆脱它。请快速查看我的TextPlay class
位置
GlobalVariables gs = (GlobalVariables) getApplication();
gs.setTestMe(getUserId);
tvUserId.setText("Copied");
是。非常感谢!
全局变量类
public class GlobalVariables extends Application {
private String testMe;
public String getTestMe() {
return testMe;
}
public void setTestMe(String testMe) {
this.testMe = testMe;
}
}
文本播放类
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>