简体   繁体   English

导入andoid.util.Log时,Android Studio无法解析符号Log

[英]Android Studio cannot resolve symbol Log when importing andoid.util.Log

I'm using libgdx and as I wanted to debug my game, I tried import Log from Android utilities to my main game file located in "core\\src\\com\\mygdx\\game\\". 我正在使用libgdx,并且想调试游戏时,我尝试将Android实用程序中的Log导入到位于“ core \\ src \\ com \\ mygdx \\ game \\”中的主游戏文件中。 For some reason AndroidStudio doesn't allow me to import that class. 由于某些原因,AndroidStudio不允许我导入该类。 Can anyone point me a solution? 谁能指出我的解决方案? Tried: rebuild project. 尝试过:重建项目。

As it is specified in the wiki under the page Interfacing with Platform-specific Code , what you need to do is provide an interface for what library you want to use, and use the interface in the core. 正如Wiki在页面与平台特定代码的接口下指定的那样,您需要做的是为要使用的库提供一个接口,并在核心中使用该接口。 And what you must do is provide the implementation from each launcher class. 您必须做的是提供每个启动器类的实现。

For example, your desktop launcher looks something like this, 例如,您的桌面启动器看起来像这样,

package com.badlogic.drop;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

    public class Main {
       public static void main(String[] args) {
          LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
          config.title = "Drop";
          config.width = 800;
          config.height = 480;
          new LwjglApplication(new Drop(), config);
       }
    }

You can see the new Drop() here - you can specify additional dependencies that you want to bind within the game, right? 您可以在此处看到new Drop() -您可以指定要在游戏中绑定的其他依赖关系,对吗?

public class Drop extends Game {
     public static final String TAG = Drop.class.getSimpleName();

     private Logger logger;

     public Drop(Logger logger) {
         this.logger = logger;
         logger.debug(TAG, "`Drop` game initialized.");
         ...
     }
}

And in the desktop it changes to 并在桌面上更改为

      new LwjglApplication(new Drop(new DesktopLogger()), config);

Where Logger is [core] Logger在哪里[核心]

public interface Logger {
    void debug(String tag, String message);
    //...
}

And desktop logger is [desktop] 桌面记录器是[desktop]

public class DesktopLogger implements Logger {
    public void debug(String tag, String message) {
        System.out.println("D/" + tag + ": " + message);
    }
}

And android logger is [android] 而android logger是[android]

public class AndroidLogger implements Logger {
    public void debug(String tag, String message) {
        Log.d(tag, message);
    }
}

So Android launcher becomes 因此,Android启动器成为

public class AndroidLauncher extends AndroidApplication {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      AndroidApplicationConfiguration config= new AndroidApplicationConfiguration();
      config.useAccelerometer = false;
      config.useCompass = false;

      initialize(new Drop(new AndroidLogger()), config);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM