-3

我创建了一个 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>
4

2 回答 2

1

尝试使用此代码初始化扩展的类对象Application

 GlobalVariables gs = (GlobalVariables) ActivityName.this.getApplicationContext();

更新答案:在 manifest.xml 中,

application标签中,使用android:name= "<your_package_name>.GlobalVariables" 不要将其声明为Activity.

于 2013-01-22T06:29:37.660 回答
0

看起来你的Application对象不是GlobalVariables。我希望您没有GlobalVariables在清单中声明成为您的应用程序对象。您需要这样做才能使类强制转换异常消失。(将类似的东西android:name=".GlobalVariables"作为属性添加到<application>标签中。)

然而!不要通过扩展来实现全局变量Application。这不是必需的; 只需创建一个单例类或具有静态数据的类。如有必要,维护一个“已初始化”标志,可在需要时检查并用于执行初始化。

文档

通常不需要子类化 Application。在大多数情况下,静态单例可以以更模块化的方式提供相同的功能。如果您的单例需要全局上下文(例如注册广播接收器),则可以为检索它的函数提供一个 Context,该上下文在首次构造单例时在内部使用 Context.getApplicationContext()。

于 2013-01-22T05:45:01.593 回答