Timer t = new Timer(player,canvas); //player объект моего игрока где координаты и свой paint
t.start();//Запускаем таймер
protected void update (Player player,Canvas canvas){
player.setX1(player.getX1() + Vx);
player.setY1(player.getY1() + Vy);
Log.d("MyTag",String.valueOf(player.getX1()));
Log.d("MyTag",String.valueOf(player.getY1()));
try {if (canvas != null){
canvas.drawCircle(player.getX1(), player.getY1(), player.getRadius(), player.getPaint());} //на этой строке происходит вылет. Canvas не NULL. По идее можно рисовать
}
catch (Exception e){Log.d("MyTag","NULL");}
}
public class Timer extends CountDownTimer {
int i;
public Player player;
public Canvas canvas;
public Timer(Player player, Canvas canvas) {
super(Integer.MAX_VALUE, 1000);
this.player=player;
this.canvas=canvas;
}
@Override
public void onTick(long millisUntilFinished) {
update(this.player,this.canvas);
i+=1;
Log.d("MyLog",Integer.toString(i));
}
@Override
public void onFinish() {
}
public class MainActivity extends AppCompatActivity {
MyDraw draw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new MyDraw(this);
setContentView(draw);
SomeThing MyThread = new SomeThing();
MyThread.run();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
class SomeThing //Мой объект, реализующий интерфейс Runnable
implements Runnable //(содержащее метод run())
{
public void run()
{
for (int i = 5; i > 0; i--) {
try {
draw.invalidate(); // вызываем
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("побочный поток! ");
}
}
}
}
public class MainActivity extends AppCompatActivity {
MyDraw draw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new MyDraw(this);
setContentView(draw);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
draw.incI();
draw.invalidate();
}
}, 500, 500);
}
class MyDraw extends View {
private int i = 0;
public MyDraw(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(i, i, i + 50, i + 50), paint);
}
public void incI() {
i += 10;
if (i > 1500)
i = 0;
}
}
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int OnePercentHeight = canvas.getHeight() / 100; // create percents value
int OnePercentWidth = canvas.getWidth() / 100;
canvas.drawColor(getResources().getColor(R.color.BackGroundColor)); // drawing backgroung
Paint PaintPlayer = new Paint(); //Settings for ball of a player
Paint PaintMainFrame = new Paint(); //settings for form rectangle
Paint PaintWallGreen = new Paint(); //Settings for green walls
PaintMainFrame.setColor(Color.GREEN);
PaintMainFrame.setStyle(Paint.Style.STROKE);
PaintMainFrame.setStrokeWidth(OnePercentWidth);
PaintWallGreen.setColor(Color.GREEN);
PaintWallGreen.setStyle(Paint.Style.FILL);
PaintPlayer.setColor(Color.WHITE);
PaintPlayer.setStyle(Paint.Style.FILL);
frame MainFrame = new frame(0 + OnePercentWidth / 2, 0 + OnePercentWidth / 2, canvas.getWidth() - OnePercentWidth / 2, getHeight() - OnePercentWidth / 2, PaintMainFrame);
Player player = new Player(OnePercentWidth * 10, canvas.getHeight() - (OnePercentHeight * 8 + OnePercentWidth * 2), OnePercentWidth * 4, PaintPlayer);
Wall RightWall = new Wall(OnePercentWidth * 40, canvas.getHeight() - OnePercentHeight, OnePercentWidth * 43, OnePercentHeight * 60, PaintWallGreen);
Log.d("MyDeb", "RightWall.getX1() " + RightWall.getX1() + " RightWall.getY1() " + RightWall.getY1() + " RightWall.getX2() " + RightWall.getX2() + " RightWall.getY2() " + RightWall.getY2());
canvas.drawRect(RightWall.getX1(), RightWall.getY1(), RightWall.getX2(), RightWall.getY2(), RightWall.getPaint()); // drawing wall
canvas.drawRect(MainFrame.getX1(), MainFrame.getY1(), MainFrame.getX2(), MainFrame.getY2(), MainFrame.getPaint()); // drawing form
canvas.drawCircle(player.getX1(), player.getY1(), player.getRadius(), player.getPaint()); // drawing player
player.setGameisrunning(true);
update(player);
System.out.println("OnDraw has worked");
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new MyDraw(this);
setContentView(draw);
SomeThing Th = new SomeThing();
Th.start();
}
class SomeThing extends Thread
implements Runnable
{
public void run()
{
for (int i = 5; i > 0; i--) { //подставляем цикл для анимации. С ним не заработает
try {
draw.updateT();
draw.invalidate();
SomeThing.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Привет из побочного потока! 1");
}
}
}
public class MyDraw extends View {
static float vy0=500;
static float vx0 =100;
public MyDraw(Context context) {
super(context);
protected static void updateT(){
vy0 += 10;
vx0 += 10;
Log.d("VX0",String.valueOf(vx0));
}
}
--------- beginning of crash
2018-11-13 07:40:50.411 17286-17457/com.smerxgames.mygameball E/AndroidRuntime: FATAL EXCEPTION: Thread-6
Process: com.smerxgames.mygameball, PID: 17286
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6953)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1120)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5207)
at android.view.View.invalidateInternal(View.java:13676)
at android.view.View.invalidate(View.java:13640)
at android.view.View.invalidate(View.java:13624)
at com.smerxgames.mygameball.MainActivity$SomeThing.run(MainActivity.java:34)
2018-11-13 07:40:50.428 1510-2807/? D/ActivityManagerInjector: ru.sberbankmobile:Metrica is killed by AMS with adj 906 ,do not allow restart
2018-11-13 07:40:50.428 1510-2807/? D/ActivityManager: cleanUpApplicationRecord -- 16783
2018-11-13 07:40:50.428 2336-2375/? I/octvm_klo: klo lock
2018-11-13 07:40:50.428 2786-2984/? I/WtProcessController: Receive am_crash event for pid: 17286 pkg = com.smerxgames.mygameball
2018-11-13 07:40:50.428 2786-2984/? W/KloServer: Aborted broadcast does not supprt for: com.smerxgames.mygameball
2018-11-13 07:40:50.429 2786-2984/? E/WtProcessController: Error pid or pid not exist
canvas.drawCircle(player.getX1(), player.getY1(), player.getRadius(), player.getPaint());
2018-11-11 18:58:54.370 3716-3716/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2018-11-11 18:58:54.370 3716-3716/? A/DEBUG: Build fingerprint: 'Xiaomi/santoni/santoni:7.1.2/N2G47H/V9.5.10.0.NAMMIFD:user/release-keys'
2018-11-11 18:58:54.371 3716-3716/? A/DEBUG: Revision: '0'
2018-11-11 18:58:54.371 3716-3716/? A/DEBUG: ABI: 'arm64'
2018-11-11 18:58:54.371 3716-3716/? A/DEBUG: pid: 3688, tid: 3688, name: ames.mygameball >>> com.smerxgames.mygameball <<<
2018-11-11 18:58:54.371 3716-3716/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x128
2018-11-11 18:58:54.371 3716-3716/? A/DEBUG: x0 0000007f8e21f130 x1 0000007f8e23cb80 x2 0000000000000070 x3 0000000000000007
2018-11-11 18:58:54.371 3716-3716/? A/DEBUG: x4 0000000000000000 x5 0000000000000000 x6 0000000072a29b74 x7 0000000000000000
2018-11-11 18:58:54.371 3716-3716/? A/DEBUG: x8 0000000000000000 x9 000000000778f6c0 x10 0000000000000001 x11 0000000000000000
2018-11-11 18:58:54.371 3716-3716/? A/DEBUG: x12 0000000000000099 x13 00000000000000f0 x14 000000000000000c x15 2e8ba2e8ba2e8ba3
2018-11-11 18:58:54.371 3716-3716/? A/DEBUG: x16 0000007f8ec2a400 x17 0000007f91662f34 x18 0000007f73d36b40 x19 0000007f7301ab00
2018-11-11 18:58:54.371 3716-3716/? A/DEBUG: x20 0000007f7301ab10 x21 0000007f8e21f130 x22 000000007b5f8dbd x23 0000007f8e23cb80
2018-11-11 18:58:54.372 3716-3716/? A/DEBUG: x24 3ddcdb95d80e8005 x25 3ddcdb95d80e8005 x26 0000007f8e240a98 x27 3ddcdb95d80e8005
2018-11-11 18:58:54.372 3716-3716/? A/DEBUG: x28 0000000000000005 x29 0000007fc2efad90 x30 0000007f8ebfc2c8
2018-11-11 18:58:54.372 3716-3716/? A/DEBUG: sp 0000007fc2efacc0 pc 0000007f8ebfc2d0 pstate 0000000020000000
2018-11-11 18:58:54.375 1764-1764/? D/VolumeDialog: DISMISS
2018-11-11 18:58:54.376 1764-1764/? D/expandable_toggle: OnToggleOrderChanged(false) (true)
2018-11-11 18:58:54.378 3716-3716/? A/DEBUG: backtrace:
2018-11-11 18:58:54.378 3716-3716/? A/DEBUG: #00 pc 00000000000952d0 /system/lib64/libhwui.so
2018-11-11 18:58:54.378 3716-3716/? A/DEBUG: #01 pc 00000000000969f8 /system/lib64/libhwui.so (_ZN7android10uirenderer15RecordingCanvas8drawOvalEffffRK7SkPaint+160)
2018-11-11 18:58:54.378 3716-3716/? A/DEBUG: #02 pc 0000000000096614 /system/lib64/libhwui.so (_ZN7android10uirenderer15RecordingCanvas10drawCircleEfffRK7SkPaint+152)
2018-11-11 18:58:54.378 3716-3716/? A/DEBUG: #03 pc 0000000000106cfc /system/lib64/libandroid_runtime.so
2018-11-11 18:58:54.378 3716-3716/? A/DEBUG: #04 pc 00000000742827f4 /data/dalvik-cache/arm64/system@framework@boot.oat (offset 0x2f5c000)
2018-11-11 18:58:54.182 3688-3688/com.smerxgames.mygameball A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x128 in tid 3688 (ames.mygameball)
--------- beginning of crash
2018-11-11 19:18:07.833 4557-4557/com.smerxgames.mygameball A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x128 in tid 4557 (ames.mygameball)
2018-11-11 19:18:07.833 1536-1557/? I/Timeline: Timeline: App_transition_ready time:22698468
2018-11-11 19:18:07.833 1536-1557/? I/Timeline: Timeline: App_transition_stopped time:22698468
2018-11-11 19:18:07.836 1536-1557/? I/ActivityManager: Displayed com.smerxgames.mygameball/.MainActivity: +871ms
2018-11-11 19:18:07.836 1536-1557/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{e63256b u0 com.smerxgames.mygameball/.MainActivity t1147} time:22698471
2018-11-11 19:18:07.837 2690-2894/? I/WtEventController: AM_Launch: 30009 TAG: 840704
2018-11-11 19:18:07.837 2690-2894/? I/WtEventController: AM_Launch: 30009 TAG: 239299583
2018-11-11 19:18:07.837 2690-2894/? I/WtEventController: AM_Launch: 30009 TAG: 25174048
2018-11-11 19:18:07.837 2690-2894/? I/WtEventController: AM_Launch: 30009 TAG: 8224
2018-11-11 19:18:07.837 2690-2894/? I/WtEventController: AM_Launch: 30009 TAG: 4096
2018-11-11 19:18:07.837 2690-2894/? I/WtEventController: AM_Launch: 30009 TAG: 10848
2018-11-11 19:18:07.837 2690-2894/? I/WtEventController: AM_Launch: 30009 TAG: 1048576
2018-11-11 19:18:07.837 1764-1764/? V/PhoneStatusBarPolicy: updateManagedProfile mManagedProfileFocused: false mManagedProfileInQuietMode: false mKeyguardVisible: false mCurrentUserId:0 mCurrentProfileId:0 mSecondSpaceStatusIconVisible: true showIcon:false
2018-11-11 19:18:07.837 1764-1764/? D/EventBus: [1764, u0] send(AppTransitionFinishedEvent)
2018-11-11 19:18:07.838 1764-1764/? D/EventBus: [1764, u0] -> ForcedResizableInfoActivityController [0x673a411, P1] onBusEvent(AppTransitionFinishedEvent)
2018-11-11 19:18:07.838 1764-1764/? D/EventBus: [1764, u0] onBusEvent(AppTransitionFinishedEvent) duration: 23 microseconds, avg: 456
2018-11-11 19:18:07.839 1536-1557/? I/WindowManager: Screen frozen for +285ms due to Window{222ecc5 u0 com.smerxgames.mygameball/com.smerxgames.mygameball.MainActivity}
2018-11-11 19:18:07.919 4585-4585/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2018-11-11 19:18:07.919 4585-4585/? A/DEBUG: Build fingerprint: 'Xiaomi/santoni/santoni:7.1.2/N2G47H/V9.5.10.0.NAMMIFD:user/release-keys'
2018-11-11 19:18:07.919 4585-4585/? A/DEBUG: Revision: '0'
2018-11-11 19:18:07.919 4585-4585/? A/DEBUG: ABI: 'arm64'
2018-11-11 19:18:07.919 4585-4585/? A/DEBUG: pid: 4557, tid: 4557, name: ames.mygameball >>> com.smerxgames.mygameball <<<
2018-11-11 19:18:07.919 4585-4585/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x128
2018-11-11 19:18:07.919 4585-4585/? A/DEBUG: x0 0000007f8e21f130 x1 0000007f8e23ccc0 x2 0000000000000070 x3 0000000000000007
2018-11-11 19:18:07.919 4585-4585/? A/DEBUG: x4 0000000000000000 x5 0000000000000000 x6 0000000072a29b74 x7 0000000000000000
2018-11-11 19:18:07.919 4585-4585/? A/DEBUG: x8 0000000000000000 x9 00000000077cc6b0 x10 0000000000000001 x11 0000000000000000
2018-11-11 19:18:07.920 4585-4585/? A/DEBUG: x12 0000000000000099 x13 00000000000000f0 x14 000000000000000c x15 2e8ba2e8ba2e8ba3
2018-11-11 19:18:07.920 4585-4585/? A/DEBUG: x16 0000007f8ec2a400 x17 0000007f91662f34 x18 0000007f73d44b40 x19 0000007f7301ab00
2018-11-11 19:18:07.920 4585-4585/? A/DEBUG: x20 0000007f7301ab10 x21 0000007f8e21f130 x22 000000007b5f8dbd x23 0000007f8e23ccc0
2018-11-11 19:18:07.920 4585-4585/? A/DEBUG: x24 3ddcdb95d80e8005 x25 3ddcdb95d80e8005 x26 0000007f8e240a98 x27 3ddcdb95d80e8005
2018-11-11 19:18:07.920 4585-4585/? A/DEBUG: x28 0000000000000005 x29 0000007fc2efad90 x30 0000007f8ebfc2c8
2018-11-11 19:18:07.920 4585-4585/? A/DEBUG: sp 0000007fc2efacc0 pc 0000007f8ebfc2d0 pstate 0000000020000000
2018-11-11 19:18:07.925 4585-4585/? A/DEBUG: backtrace:
2018-11-11 19:18:07.925 4585-4585/? A/DEBUG: #00 pc 00000000000952d0 /system/lib64/libhwui.so
2018-11-11 19:18:07.926 4585-4585/? A/DEBUG: #01 pc 00000000000969f8 /system/lib64/libhwui.so (_ZN7android10uirenderer15RecordingCanvas8drawOvalEffffRK7SkPaint+160)
2018-11-11 19:18:07.926 4585-4585/? A/DEBUG: #02 pc 0000000000096614 /system/lib64/libhwui.so (_ZN7android10uirenderer15RecordingCanvas10drawCircleEfffRK7SkPaint+152)
2018-11-11 19:18:07.926 4585-4585/? A/DEBUG: #03 pc 0000000000106cfc /system/lib64/libandroid_runtime.so
2018-11-11 19:18:07.926 4585-4585/? A/DEBUG: #04 pc 00000000742827f4 /data/dalvik-cache/arm64/system@framework@boot.oat (offset 0x2f5c000)
2018-11-11 19:18:08.211 1536-4586/? W/ActivityManager: Force finishing activity com.smerxgames.mygameball/.MainActivity
2018-11-11 19:18:08.211 2690-2894/? I/WtProcessController: Receive am_crash event for pid: 1536 pkg = com.smerxgames.mygameball
2018-11-11 19:18:08.211 2690-2894/? W/KloServer: Aborted broadcast does not supprt for: android
2018-11-11 19:18:08.212 2314-2355/? I/octvm_klo: get event file: /data/tombstones/tombstone_05
2018-11-11 19:18:08.213 2314-2355/? I/octvm_klo: klo lock
2018-11-11 19:18:08.213 1536-1548/? I/BootReceiver: Copying /data/tombstones/tombstone_05 to DropBox (SYSTEM_TOMBSTONE)
2018-11-11 19:18:08.217 1536-4586/? D/ActivityTrigger: ActivityTrigger activityPauseTrigger
2018-11-11 19:18:08.222 2314-2355/? I/octvm_klo: summary:[signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x128
0 miui.os.DropBoxManager.addText:314