使用Singleton Patterns for Android Project的NoClassDefFoundError

时间:2014-04-28 21:53:56

标签: java android maven singleton noclassdeffounderror

我目前正在开发一个Android应用程序,该应用程序使用RESTful Web服务与作为Maven项目的服务进行通信。我们使用Singleton Pattern来存储我们从Maven Project获得的ResponseBeans。但是,只要在SingletonClass中访问ResponseBean,我就会得到一个NoClassDefFoundError,即使我已经将Maven项目添加到Java Build Path中。我一整天都试图解决这个问题,但我无法找到解决方案。

这是我收到的错误消息:

04-28 21:17:27.889: E/AndroidRuntime(1584): FATAL EXCEPTION: main
04-28 21:17:27.889: E/AndroidRuntime(1584): java.lang.NoClassDefFoundError: ch.uzh.ifi.seal.soprafs14.common.beans.user.UserResponseBean
04-28 21:17:27.889: E/AndroidRuntime(1584):     at ch.uzh.ifi.seal.soprafs14.group_09_android.model.GameDataSingleton.<init>(GameDataSingleton.java:32)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at ch.uzh.ifi.seal.soprafs14.group_09_android.model.GameDataSingleton.getInstance(GameDataSingleton.java:45)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at ch.uzh.ifi.seal.soprafs14.group_09_android.gui.activities.MenuActivity.onCreate(MenuActivity.java:55)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at android.app.Activity.performCreate(Activity.java:5104)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at android.os.Looper.loop(Looper.java:137)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at java.lang.reflect.Method.invoke(Method.java:511)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-28 21:17:27.889: E/AndroidRuntime(1584):     at dalvik.system.NativeStart.main(Native Method)
04-28 21:17:31.293: I/Process(1584): Sending signal. PID: 1584 SIG: 9

这是我得到异常的类

    package ch.uzh.ifi.seal...;

import ch.uzh.ifi.seal.soprafs14.common.beans.lobby.LobbyResponseBean;
import ch.uzh.ifi.seal.soprafs14.common.beans.user.UserResponseBean;

/**
 * This singleton class contains all game data on the client and allows to
 * access this data from all activities in the application.
 * 
 */
public class GameDataSingleton {
    private static GameDataSingleton instance = null;

    private String username;

    /**
     * Contains user data like ID, username and token.
     */
    private UserResponseBean user;

    /**
     * Contains lobby data like lobbyID and a list of all players.
     */
    private LobbyResponseBean lobby;

    /**
     * Private Constructor -> Singleton pattern
     */
    private GameDataSingleton() {
        user = new UserResponseBean();  // I get the Error at this line
        lobby = new LobbyResponseBean();
        username = "";
    }

    /**
     * Returns instance of GameDataSingleton or if it does not yet exist,
     * creates a new instance.
     * 
     * @return GameDataSingleton instance
     */
    public static GameDataSingleton getInstance() {
        if (instance == null) {
            instance = new GameDataSingleton();
        }
        return instance;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public UserResponseBean getUser() {
        return user;
    }

    public void setUser(UserResponseBean user) {
        this.user = user;
    }

    public LobbyResponseBean getLobby() {
        return lobby;
    }

    public void setLobby(LobbyResponseBean lobby) {
        this.lobby = lobby;
    }
}

提前致谢!

2 个答案:

答案 0 :(得分:1)

您会收到此异常,因为DVM无法在您的apk中找到UserResponseBean。

尝试重新检查你的maven依赖关系(我假设你正在使用maven)并确保它是正确的。

如果您使用.jar库,请尝试将其移至libs文件夹。

让我知道会发生什么。

答案 1 :(得分:0)

尝试属性 - &gt; Java构建路径 - &gt;库 - >添加库 - &gt;用户库!!!!!!我认为这是你的解决方案。