简体   繁体   中英

Error When trying to add a image on android studio (java)

My script is post to show a image each time I run it on android studios but I keep on getting an error each time I try to run the script it's post to show funny picture of trumps face. I am not really sure why I keep on getting an error each time i run the script because it works when I put in a image of a smiley face.

Here's the script:

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
}

Here's the error: enter image description here

There is proper protocol you must follow to be able to use the image in android coding. Which involves using resources files. Please follow this guide to better acquaint yourself with adding resources (in this case images) to your android project.

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

You have to put the file Trump.jpg under the assets folder of the android project. Did you do that? Based on the exception it isn't there.

And use the command

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

to initialize the Texture object.

If it's still not working clean your projects. I don't know Android Studio, but in Eclipse you can select the projects and click on Project/Clean to clean and rebuild the application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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