【问题标题】:Error When trying to add a image on android studio (java)尝试在 android studio (java) 上添加图像时出错
【发布时间】:2016-05-21 15:14:55
【问题描述】:

我的脚本每次在 android studio 上运行时都会显示图像,但每次尝试运行脚本时都会出现错误,它是为了显示王牌面孔的有趣图片而发布的。我不确定为什么每次运行脚本时都会出现错误,因为当我输入笑脸图像时它会起作用。

这是脚本:

package com.udacity.gamedev.logging;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class LoggingDemo extends ApplicationAdapter {

    // TODO: Give your ApplicationListener a log TAG]
    public static final String TAG = LoggingDemo.class.getName();


    SpriteBatch batch;
    Texture img;

    @Override
    public void create() {
        batch = new SpriteBatch();
        img = new Texture("Trump.jpg");

        // TODO: Use Gdx.app to find what ApplicationType we're running
        // TODO: Use Gdx.app to log the result
        Gdx.app.log(TAG, "We're running on" +
                Gdx.app.getType()

        );

    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(img, 0, 0);
        batch.end();
    }

    // TODO: Run the Desktop app and find your log message
    // TODO: Run the Android app and find your log message
}

这是错误: enter image description here

【问题讨论】:

    标签: java android-studio libgdx android-image


    【解决方案1】:

    您必须遵循适当的协议才能在 android 编码中使用图像。这涉及使用资源文件。请按照本指南更好地了解如何将资源(在本例中为图像)添加到您的 android 项目。

    http://www.higherpass.com/android/tutorials/working-with-images-in-android/

    【讨论】:

      【解决方案2】:

      您必须将文件Trump.jpg 放在android 项目的assets 文件夹下。是你做的吗?根据例外情况,它不存在。

      并使用命令

      new Texture(Gdx.files.internal("Trump.jpg"));
      

      初始化纹理对象。

      如果它仍然无法正常工作,请清理您的项目。我不知道 Android Studio,但是在 Eclipse 中你可以选择项目并点击 Project/Clean 来清理和重建应用程序。

      【讨论】:

        猜你喜欢
        • 2018-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-23
        • 1970-01-01
        • 1970-01-01
        • 2016-03-25
        • 2020-04-10
        相关资源
        最近更新 更多