我如何摆脱这个类强制转换异常错误在我的代码



我创建了一个GlobalVariables类,以便在多个类之间共享变量。但我现在有问题与classcastexception。我一直在阅读这个异常,我知道我不能将主类的实例转换为子类。仍然不确定如何在我的代码中改变它来摆脱它。请快速查看我的TextPlay class

        GlobalVariables gs = (GlobalVariables) getApplication();
        gs.setTestMe(getUserId);
        tvUserId.setText("Copied"); 

。非常感谢!

<

Globalvariables类/strong>

public class GlobalVariables extends Application {
private String testMe;
public String getTestMe() {
    return testMe;
}
public void setTestMe(String testMe) {
    this.testMe = testMe;
}
}
<

TextPlay类/strong>

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>

尝试以下代码初始化扩展Application的类对象

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

更新在manifest.xml,

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

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

然而

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

From the docs:

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

相关内容

  • 没有找到相关文章

最新更新