【问题标题】:Android, App42API & Libgdx - App crashing after adding app42api external jars (dependencies)Android、App42API 和 Libgdx - 添加 app42api 外部 jar(依赖项)后应用程序崩溃
【发布时间】:2015-05-22 22:23:02
【问题描述】:

我正在使用 Android、libgdx、app42api 和平铺地图在 Eclipse 中开发 2d 游戏。我目前正在使用 app42api 的用户服务实现登录系统。我可以成功注册并登录一个帐户,登录后我正在从该帐户加载我的主播放屏幕。修复先前的错误后,我被告知添加一些外部 jar,app42api 的服务需要正常运行。我添加了以下罐子:httpcore-4.1.jar、httpcore-1.1.1.jar、commons.logging-1.1.1、commons-logging-api-1.1.1。在最初添加罐子后,我的应用程序运行良好,登录帐户并加载了播放屏幕。在弄乱了我的想法之后,我突然遇到了以下错误,现在我根本无法运行我的 android 应用程序。

错误:[2015-05-22 17:55:05 - wurfle-gdx-android] Dx 警告:忽略匿名内部类的 InnerClasses 属性 (org.apache.commons.logging.impl.WeakHashtable$1) 不附带 关联的 EnclosureMethod 属性。这个类可能是由一个 不针对现代 .class 文件格式的编译器。推荐的 解决方案是使用最新的编译器从源代码重新编译类 并且没有指定任何“-target”类型选项。忽视的后果 此警告是此类上的反射操作将错误地 表明它不是一个内部类。 [2015-05-22 17:55:19 - Dex Loader] 无法执行 dex:多个 dex 文件定义 Lorg/apache/commons/logging/Log; [2015-05-22 17:55:19 - wurfle-gdx-android] 转换为 Dalvik 格式失败:无法执行 dex:多个 dex 文件定义 Lorg/apache/commons/logging/Log;

我可以在桌面上运行我的应用程序,但我还没有初始化 app42api 连接,所以桌面不会运行 api。我已经尝试添加和删除 jars,清理我的项目并重新启动我的 cp(人们就其他与此问题相关的类似问题提出了一些建议),但没有任何效果。

我将附上我正在使用的一些课程。

AndroidLauncher.java:

package com.wurfle.game.android;

import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.shephertz.app42.paas.sdk.android.App42API;
import com.wurfle.game.MainGameLoop;
import com.wurfle.game.network.App42Handler;

public class AndroidLauncher extends AndroidApplication 
{

    @Override
    protected void onCreate (Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        App42API.initialize(getContext(), App42Handler.apiKey, App42Handler.secretKey);
        System.out.println("initialized app42api...");
        initialize(new MainGameLoop(), config);


    }
}

登录.java:

package com.wurfle.game.network;

import com.badlogic.gdx.Game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.shephertz.app42.paas.sdk.android.App42Exception;
import com.shephertz.app42.paas.sdk.android.user.UserService;
import com.wurfle.game.States.Play;


public class Login implements Screen
{

    private TextField usernameTxtField, passwordTxtField;
    private TextButton loginBtn;
    private Skin loginSkin;
    private Stage stage;
    private Table table;
    private TextFieldStyle txtFieldStyle;
    private TextureAtlas loginAtlas;
    private BitmapFont font;
    public static String username;

    @Override
    public void render(float delta) 
    {
        Gdx.gl.glClearColor(1,1,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(delta);

        stage.draw();
    }

    @Override
    public void show() 
    {

        stage = new Stage();
        // set input processor to stage element
        Gdx.input.setInputProcessor(stage);

        table = new Table();

        table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());


        font = new BitmapFont();
        txtFieldStyle = new TextFieldStyle();
        txtFieldStyle.fontColor = Color.RED;
        txtFieldStyle.font = font;

        usernameTxtField = new TextField("", txtFieldStyle);
        passwordTxtField = new TextField("", txtFieldStyle);


        // set size of text fields
        usernameTxtField.setSize(100, 25);
        passwordTxtField.setSize(100, 25);

        // register button
        loginAtlas = new TextureAtlas("resmenu/menu/loginBtn.pack");
        loginSkin = new Skin(loginAtlas);

        // new style for exit btn
        TextButtonStyle loginButtonStyle = new TextButtonStyle();

        // when user clicks on button, load new image, when he lets go reload
        // original image
        loginButtonStyle.up = loginSkin.getDrawable("menuLoginBtn");
        loginButtonStyle.down = loginSkin.getDrawable("menuLoginBtnPressed");

        // off set btn
        loginButtonStyle.pressedOffsetX = 1;
        loginButtonStyle.pressedOffsetY = -1;

        loginButtonStyle.font = font;

        loginBtn = new TextButton("", loginButtonStyle);
        // add new listener
        loginBtn.addListener(new ClickListener() 
        { // fire event
            public void clicked(InputEvent event, float x, float y) 
            {
                if(usernameTxtField.getText().equals("") || passwordTxtField.getText().equals("")) 
                {

                    System.out.println("need to input sumn..");
                }
                else
                {
                    loginAccount(usernameTxtField.getText().trim(), passwordTxtField.getText().trim());
                }

            }
        });

        // add actors to table
        table.center();
        table.row();
        table.add(usernameTxtField);
        table.row();
        table.add(passwordTxtField);
        table.row();
        table.add(loginBtn);
        table.debug();

        // add table to stage
        stage.addActor(table);

    }

    public void loginAccount(String usr, String pw)
    {
        Login.username = usr;

        try
        {

            UserService us = new UserService(App42Handler.apiKey, App42Handler.secretKey);

            // if username & password exists
            if(us.authenticate(usr, pw) != null)
            {
                System.out.println("account authenticated: " + usr + " just logged in");
                ((Game)Gdx.app.getApplicationListener()).setScreen(new Play());
                dispose();

            }
        }
        catch(App42Exception e)
        {
            e.printStackTrace();
        }

    }


    @Override
    public void resize(int width, int height) 
    {
        // TODO Auto-generated method stub

    }


    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }


    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }


    @Override
    public void hide() {
        // TODO Auto-generated method stub

    }


    @Override
    public void dispose() 
    {

        loginSkin.dispose();
        loginAtlas.dispose();

        font.dispose();

        loginBtn.getListeners().clear();

        stage.dispose();

    }

}

我已确保我的 jar 也已正确添加到 android 构建路径中。我一直在尝试解决这个问题一段时间,所以任何帮助表示赞赏。

谢谢, 德文郡。

【问题讨论】:

    标签: android login libgdx app42


    【解决方案1】:

    在解决您分享的错误后,我发现了一些情况,例如:

    1. 多个dex文件定义Lorg/apache/commons/logging/Log; [2015-05-22 17:55:19 - wurfle-gdx-android] 转换为 Dalvik 格式失败:无法执行 dex:多个 dex 文件定义 Lorg/apache/commons/logging/Log;添加了两个相同的库。

    2. 当您在主 UI 线程 上使用 us.authenticate(usr, pw) 网络 API 时。我想建议你使用我们的异步 API

      用户服务 userService = App42API.buildUserService();
      userService.authenticate(userName , pwd, new App42CallBack() {
      public void onSuccess(对象响应)
      {
      用户用户=(用户)响应;
      System.out.println("用户名是" + user.getUserName());
      System.out.println("sessionId 为" + user.getSessionId());
      }
      公共无效 onException(异常前)
      {
      System.out.println("异常信息:"+ex.getMessage());
      }
      });
      收到响应后,请使用 runOnUIThread 更新 UI 线程。

    3. 还请分享一些 logcat 日志,以帮助我们跟踪确切的问题。

    【讨论】:

    • 谢谢!我注意到的一件事是,当我添加这 4 个特定的 jar(常见的、http 等)时,它会自动将它们添加到我的私有库部分,而无法删除它们。我删除了这 4 个罐子,我可以注册并登录到 app42api 的...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 2018-06-01
    • 1970-01-01
    • 2023-03-25
    • 2020-03-07
    • 1970-01-01
    相关资源
    最近更新 更多