This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am new to android programming and am trying to draw a circle on the screen. Eventually I'd like to have the circle move around. But for now I have been having trouble getting the circle to draw. When I run the code, it gives me an error:
"Attempt to invoke virtual method 'long
android.graphics.Paint.getNativeInstance()' on a null"
My Code:
package com...
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View {
private Bitmap b;
public Paint p;
public MyView(Context context) {
super(context);
Paint p = new Paint();
p.setColor(Color.GREEN);
b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(50, 50, 10, p);
}
}
}
Problem with your code is
public MyView(Context context) {
super(context);
Paint p = new Paint();// Problem is here this is local declaration
p = new Paint() // do like this and remove above line
p.setColor(Color.GREEN);
b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
}
Related
I'm creating a text editing app that allows the user to edit text using different fonts, colors, sizes, etc. Right now I'm working on a feature that is supposed to curve the text. I already tried asking once HERE but the answer that was posted wasn't really what I was looking for (or maybe it was and I just don't understand exactly what they meant) so I figured I'd ask again with an ACTUAL reference to what I'm trying to accomplish. So as you can see, I'm trying to have the text curve based on what the valued of the slider (seekbar in my case) is, but I just don't know how to do it. I was trying to mimic what is done HERE specifically the Along a path part, but again I'm not sure how to make it work with a seekbar as shown in the image.
Thank you
EDIT
Here's the code that I've been working with. Like I stated earlier, this my be exactly what I'm looking for and I'm just stupid, but here it is anyways.
import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import android.view.*;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Painting extends Activity
{
public static int y = 0;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View
{
private Paint mPaint;
private float mX;
private float[] mPos;
private Path mPath;
private Paint mPathPaint;
private static final int DY = 30;
private static final String TEXTONPATH = "Along a path";
private static void makePath(Path p)
{
p.moveTo(10, 0);
p.cubicTo(00, 00, 00, 00, 900, 00);
}
public SampleView(Context context)
{
super(context);
setFocusable(true);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(90);
mPaint.setTypeface(Typeface.SERIF);
mPath = new Path();
makePath(mPath);
mPathPaint = new Paint();
mPathPaint.setAntiAlias(true);
mPathPaint.setColor(0x800000FF);
mPathPaint.setStyle(Paint.Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.WHITE);
Paint p = mPaint;
float x = mX;
float y = 0;
float[] pos = mPos;
p.setColor(0x80FF000);
canvas.drawLine(x, y, x, y+DY*3, p);
p.setColor(Color.BLACK);
canvas.translate(0, DY*10);
canvas.drawPath(mPath, mPathPaint);
p.setTextAlign(Paint.Align.LEFT);
canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);
}
}
}
You are not going to be able to do this with a TextView. Instead you will have to write you own class extending View.
You will have to override onDraw and draw the text onto the Canvas there.
To make it work with a SeekBar you can use SeekBar#setOnSeekBarChangeListener. There in onProgressChanged you can tell your View to change its appearance.
I'm writing a very simple Android drawing program, for which I wrote a class DoodleCanvas.java, which includes (at the very bottom) a function to clear the canvas by filling it with white.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class DoodleCanvas extends View {
private Paint mPaint; //Paint object for setting attr
private Path mPath; //Path object for tracing Paint over
public Canvas canvas; //Canvas object for calling Paint
public DoodleCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(10);
mPath = new Path();
}
//Draw mPaint over mPath
#Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(mPath, mPaint);
super.onDraw(canvas);
}
//Create mPath from touch events
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mPath.moveTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
mPath.lineTo(event.getX(), event.getY());
invalidate();
break;
}
return true;
}
//Class function for filling canvas with white, effectively cleaning it
public void clearcanvas(Canvas canvas) {
canvas.drawColor(Color.WHITE);
}
}
In my MainActivity.java, I have set up a clear button to call clearcanvas.
package brianslho.basiccanvas;
import android.graphics.Canvas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
DoodleCanvas dcanvas; //Custom class DoodleCanvas (from "DoodleCanvas.java")
Button btn; //Button for clearing screen
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dcanvas = findViewById(R.id.canvas);
btn = findViewById(R.id.btn);
btn.setOnClickListener(clearcanvas);
}
//Clear screen on button click
protected Button.OnClickListener clearcanvas = new View.OnClickListener() {
#Override
public void onClick(View v) {
//ERROR: canvas is null
Canvas canvas = dcanvas.canvas;
dcanvas.clearcanvas(canvas);
}
};
}
The app crashes whenever I press the clear button. The error message is, in essence, java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Canvas.drawColor(int)' on a null object reference. I've declared the canvas inside onClick, which I'm very sure is not "good coding", but just as an attempt to fix the null problem. I've searched StackOverflow for similar problems (and boy there are a LOT) but couldn't find anything that'd solve this.
Thank you in advance for your help!
Your problem is you have declare variable with the same name: public Canvas canvas; vs public void clearcanvas(Canvas canvas) => program will understand that canvas in your function is the global instead of the parameter of function. => solution: change variable name
you have to call the constructor and you have declared variable with the same name public Canvas canvas; and public void clearcanvas(Canvas canvas).So change the variable.
dcanvas = new DoodleCanvas(Context context, AttributeSet attrs);
I have a custom view and I overrided the ondraw. When I run my program the method invalidate updates the ondraw on my emulator but it doesn't work on real phone at all and the view is fixed on phone.
Anybody knows why?
here is my code:
package com.example.canvas;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.view.View;
public class Myview extends View{
String second="0";
int r=100;
public Myview(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
Paint paint=new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(12);
canvas.drawLine(getWidth()/2f, getHeight()/2f,getWidth()/2f+ ((r-18.0f)*(float)Math.cos(Math.toRadians((Float.valueOf(second)/60f*360f) -90.0f))) ,getHeight()/2f+((r-18.0f)*(float)Math.sin(Math.toRadians ((Float.valueOf(second)/60f*360f)-90.0f))), paint);
update();
super.onDraw(canvas);
}
private void update() {
Handler h=new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
Calendar c=Calendar.getInstance();
SimpleDateFormat ss=new SimpleDateFormat("ss");
second=ss.format(c.getTime());
invalidate();
}
}, 1000);
}
}
Bingo!when I set timezone for calendar object to my country,invalidate worked and ondraw was updated on emulator and real phone.
The code will run through an array. If the character 'X' is seen in the array, it will drawRect at the respective spot on the screen.
The problem is that the color isn't actually being drawn on the screen. Some of the android websites talked about how you need a canvas, bitmap, etc. etc.
My question: What do I need to do to get the colours from the drawRect method to actually show up on the screen?
package com.example.routedrawingtest;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.graphics.Paint;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
float scale_x;
float scale_y;
public void main(String[] args){
#SuppressWarnings("unused")
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
int screen_width = size.x;
int screen_height = size.y;
scale_x = screen_width/125;
scale_y = screen_height/100;
}
public void onDraw(Canvas canvas) {
int i;
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
int j;
for (i=0; i<125; i++)
for (j=0; j<100; j++)
if (charMap[i][j]=='X')
canvas.drawRect(i*scale_x, j*scale_y, scale_x,scale_y, paint);
}
}
The method onDraw is not called by an Activity to do what you want, you need to extend the class View and implement the method there. Until you learn more when you add the view to a parent, you have to give it an explicit size like fill_parent or it will not show up on the screen.
For example:
public class Drawer extends View
{
Paint paint = new Paint();
public Drawer()
{
paint.setStyle(Style.STROKE);
paint.setStrokeColor(Color.BlUE);
// etc...
}
public void onDraw(Canvas canvas)
{
for (i=0; i<125; i++)
for (j=0; j<100; j++)
if (charMap[i][j]=='X')
canvas.drawRect(i*scale_x, j*scale_y, scale_x,scale_y, paint);
}
}
Add the view to your code via XML or via code like using it's fully qualified name:
XML:
<com.example.comp.Drawer
android:layout_width="fill_parent"<<<< specify a size
etc...
/>
i am making an app to make the hands of a clock.That is, i draw a simple line on the screen and want it rotate like the hands of a clock.how do i do this using a timer?(so that the position of the line refreshes).Only one end point of the line should rotate,while the other remains staionary.In the folllowing code,i only draw a line on the screen:
DrawView.java:
package LineRefresh.xyz.com;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
#Override
public void onDraw(final Canvas canvas) {
paint.setColor(Color.BLACK);
canvas.drawLine(50, 200, 270, 200, paint);
}
}
LineRefresh.java:
package LineRefresh.xyz.com;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class LineRefresh extends Activity {
DrawView drawView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
Try something like this:
private Handler mHandler = new Handler();
private Runnable MethodToDrawClockLine = new Runnable()
{
public void run()
{
//execute here your drawing routine.
// after drawing set the timer for another x period of time
mHandler.postDelayed(MethodToDrawClockLine, 1000); //it'll re-execute it after 1sec.
}
}
in you OnCreate or any method where you want to Start the Timer you can use:
mHandler.removeCallbacks(mUpdateTimeTask); //be sure to remove any handler before add a new one
mHandler.postDelayed(MethodToDrawClockLine, 1000); //it'll execute it after 1sec.
when you're done, remember to remove the handler.
mHandler.removeCallbacks(mUpdateTimeTask);
That shold be it.