我试图在Activitie的布局上绘制GridView,但没有触发public class StrangeCalculation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is the first number?");
int num1 = input.nextInt();
System.out.println("What is the second number?");
int num2 = input.nextInt();
int min = Math.min(num1, num2);
int max = Math.max(num1, num2);
int sum = min + max;
int diff = max - min;
// Main logic
double strangeValue = (Math.pow(10, (sum + "").length()) * diff + sum);
}
}
方法。什么可能是错的?
onDraw()
package com.example.twinkle94.lab_work;
import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.GridView;
public class GameView extends GridView
{
private int background_color;
private int player_color;
private int wall_color;
private int[][] game_field;
private GameViewAdapter adapter;
public GameView(Context context)
{
super(context);
setWillNotDraw(false);
}
public GameView(Context context, @Nullable AttributeSet attrs)
{
super(context, attrs);
setWillNotDraw(false);
background_color = ContextCompat.getColor(context, R.color.background_color);
player_color = ContextCompat.getColor(context, R.color.player_color);
wall_color = ContextCompat.getColor(context, R.color.wall_color);
game_field = new int[3][3];
adapter = new GameViewAdapter(context);
this.setAdapter(adapter);
//TODO: change to dynamic!
setNumColumns(3);
//TODO: change to dynamic!
game_field[0][0] = player_color;
game_field[0][1] = player_color;
game_field[0][2] = wall_color;
game_field[1][0] = background_color;
game_field[1][1] = wall_color;
game_field[1][2] = player_color;
game_field[2][0] = wall_color;
game_field[2][1] = background_color;
game_field[2][2] = player_color;
}
public GameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
setWillNotDraw(false);
}
public void onUpdate()
{
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
for(int i = 0; i < game_field.length; i++)
{
for(int j = 0; j < game_field[i].length; j++)
{
adapter.add(game_field[i][j]);
adapter.update();
}
}
}
public void draw()
{
postInvalidate();
}
}
package com.example.twinkle94.lab_work;
import android.content.Context;
public class Game
{
private Context context;
private GameView gameView;
public Game(Context context)
{
this.context = context;
gameView = (GameView) ((MainActivity)context).findViewById(R.id.gameView);
}
public void run()
{
while(true)
{
gameView.draw();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
package com.example.twinkle94.lab_work;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Game(this).run();
}
}
感谢您的帮助,谢谢!
答案 0 :(得分:0)
我已经解决了。问题是无限循环。它阻止了UI线程显示视图。