Drawing an image in android - java

I'm trying to just draw a simple image from the drawable-mdpi folder in android but I keep getting an error saying droid_1 cannot be resolved or is not a field, droid_1 is the name of the image, this is my code,
thanks for any help.
import android.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
public class GameView extends View {
private Bitmap bmp;
public GameView(Context context) {
super(context);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, 10, 10, null);
}
}

Change "import android.R;" to "import "import com.your.package.R;" where com.your.package is your package name...

You are importing android.R the android.R file won't contain your resources

You say that your image is in the drawable-mdpi folder. If you aren't using an MDPI display, Android won't be able to find an image to show on the screen. Make sure you have an image in each of the drawable-... folders, or a single image in the drawable folder, so that at least it will be able to find an image to use regardless of the screen resolution.
Also, you're trying to import the standard android.R package. Try removing this import line completely and let Eclipse suggest the correct R package to import - it'll be something like import com.yourapp.R;

Related

The constructor BorderStroke(Color, BorderStrokeStyle, null, null) is undefined

I am trying to give my Hbox a Border but in my project, it gives an error back. DonĀ“t understand because in my friend's project it works perfectly.
`HBox hbox= new HBox();
hbox.setBorder(new Border(new BorderStroke(Color.GREEN, BorderStrokeStyle.SOLID, null, null)));
hbox.setSpacing(50);
hbox.setPadding(new Insets(20));
hbox.getChildren().add(imv);
hbox.getChildren().add(label);`
I found that if you import the wrong Color you will get the same error back:
//WRONG IMPORTS
import java.awt.Color;
//OR
import com.sun.prism.paint.Color;
The constructor BorderStroke(Color, BorderStrokeStyle, null, null) is undefined
This is easy to mess up because Eclipse will automatically recommend any of the 3 Color imports as options.
Remove the incorrect import for Color that you currently have, and instead make sure you manually import all the correct Classes at the top of your current Class so you do not use the wrong one:
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
Notice this is now javafx.scene.paint.Color instead.

cannot resolve symbol PreviewCallback

I created a class in java for capturing pictures from a phone camera and I get the following error cannot resolve the PreviewCallback symbol.
Because you are using incorrect Camera class.
First remove this line
import android.graphics.Camera;
Then add this line
import android.hardware.Camera;

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.

Red line error with correct code

image
Well i have this problem when the code is correct, but it gives me red line error!
can anybody help me.
Main.java
package com.example.sout;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import
~
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
ImageView iv = (ImageView) findViewById(R.id.img1);
iv.setBackgroundResource (R.anim.animation);
~~~~~~~~~~~~~~~~
iv.setOnClickListener(new OnClickListener());
~~~~~~~~~~~~~~~ ~
}
in (note the ~ characters above):
iv.setBackgroundResource (R.anim.animation);
iv.setOnClickListener(new OnClickListener());
The setBackgroundResource and setOnClickListener are red too.
The error in iv.setBackgroundResource (R.anim.animation); comes because that method takes integer as a parameter .So change it to the following,
iv.setBackgroundResource (R.drawable.image1); //image1 is a drawable which is inside your drawable folder.
and the second error comes because you have not imported the required package and have not overridden the methods required for that. Change it to the following,
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do your task here
}
});
after changing press ctrl+shift+p to import the required packages.
Sometimes, at least with Eclipse, it gets it into it head that there's an error and it won't let it go. But, with Eclipse, you can at least put the cursor over a marker and it will explain what the problem is (or what it thinks it is). I'd try to hover over the errant lines to see if a popup box appears telling you what it thinks.
If you're sure it's wrong, often I've found that saving the file (or all files) will fix it. Sometimes adding then deleting a space on the errant line will fix it.
However, I notice that you have an incomplete import at the top of your file and this may be preventing the syntax checker from properly analysing the source file.
My advice is to fix that first, then try those other two tricks (save then, if that doesn't work, edit and undo on the errant line). Hopefully that will make it disappear.
Of course, you may want to check, just in case, your R.java file to ensure that member exists in there somewhere. I've been bitten by my own misspellings before.

Clean deleted R. Cant run any of my codeI

I did clean project and now i cannot run any of my code. Any files that has to do with R, just says cannot be resolved as a field. I've looked at so many other stackoverflow posts with similar problems but none seems to actually help. Help please!!
I have about 15 classes but here is an example
package com.Class;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class NewAccount extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
}
}
If R.java is not being generated, chances are there are some other errors in your code than the ones referring to the R file. Find these errors, solve them, and then R will be regenerated.
Did you import the R file?
import com.example.myapp.R;
This may be useful too: R cannot be resolved - Android error

Categories