0

我有这个异常“java.lang.ClassCastException:android.app.Application 无法转换为 com.utility.TablePlussAndroid”我不明白为什么......非常感谢您的帮助

package com.tableplustablet;
private com.utility.TablePlusAndroid session;

public class LoginActivity extends Activity {

    // ...

    // get session from Application                        
    session =  (TablePlusAndroid) this.getApplication(); // <--EXCEPTION
    session.setUserKey(userKey);
} 

// -------------------------------------------------------------

package com.utility;
import android.app.Application;

 //Save User Data in  application's context
 public class TablePlusAndroid extends Application{
private Long userKey;
private String userName;
private Long currentTableKey;  

    public Long getCurrentTableKey() {
        return currentTableKey;
}

    public void setCurrentTableKey(Long currentTableKey) {
        this.currentTableKey = currentTableKey;
    }
    public Long getUserKey(){ return userKey;}
    public void setUserKey(Long key){ userKey=key;}
    public String getUserName() { return userName; }
    public void setUserName(String userName) { this.userName = userName; }  
}
4

1 回答 1

4

您需要在 AndroidManifest.xml 文件中使用您的应用程序类。

它应该如下所示:

<application android:name="application.package.and.class">
    <activity .... />
</application>
于 2012-12-20T17:34:44.070 回答