android - Null Pointer Exception at calling postInvalidate method in thread class -
i have created custom thread
, used postinvalidate
method recalling , move image there null pointer exception
@ thread class animthread postinvalidate
.
package com.example.ahmad.ballmoving; import android.view.view; /** * created ahmad on 7/30/2015. */ public class animthread extends thread { private view threadview; public animthread(view view ) { if(view!=null) { threadview = view; } } @override public void run() { while(true) { try { thread.sleep(1000); threadview.postinvalidate(); } catch (interruptedexception e) { e.printstacktrace(); } } } }
this custom view class .
package com.example.ahmad.ballmoving; 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.graphics.rect; import android.util.attributeset; import android.view.view; /** * created ahmad on 7/29/2015. */ public class mycontextview extends view { int xvelocity=1; int yvelocity=1; private runnable runn; bitmap imag1; int x=0,y=0; public mycontextview(context context, attributeset att) { super(context); imag1=bitmapfactory.decoderesource(getresources(),r.drawable.images); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); x=x+xvelocity; y=y+yvelocity; //canvas.save(); //invalidate(); canvas.drawbitmap(imag1, x, y, null); } }
this main class
package com.example.ahmad.ballmoving; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; public class mainactivity extends actionbaractivity { private thread thread1; private thread thread2; public mycontextview mcv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mycontextview c = (mycontextview)findviewbyid(r.id.ko); thread = new animthread(c); a.start(); } }
this xml of main class :
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <com.example.ahmad.ballmoving.mycontextview android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/ko"/> </relativelayout>
can please try countdowntimer ?
new countdowntimer(1000,100) { @override public void ontick(long millisuntilfinished) { } @override public void onfinish() { threadview.postinvalidate(); } }.start();
hope you.
Comments
Post a Comment