当我按下主页然后回到它时,我的应用程序保持着FC logcat给了我没有错误:
D/gralloc_goldfish(1842): Emulator without GPU emulation detected.
I/Choreographer(1842): Skipped 60 frames! The application may be doing too much work on its main thread.
I/dalvikvm(1842): threadid=3: reacting to signal 3
I/dalvikvm(1842): Wrote stack traces to '/data/anr/traces.txt'
这是一个非常示范的应用程序,因为我不关心谁使用它,所以生病了整个代码:
public class MainActivity extends Activity {
dartBoard board;
EditText radius;
EditText numDarts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
board = new dartBoard(this);
Display mDisplay = this.getWindowManager().getDefaultDisplay();
int width= mDisplay.getWidth();
int Height= mDisplay.getHeight();
LinearLayout lin = new LinearLayout(this);
LinearLayout linMain = new LinearLayout(this);
Button throwDart = new Button(this);
Button setRadius = new Button(this);
setRadius.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
setRadius.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
setRadius.setText("Set Radius");
setRadius.setTextSize(10);
setRadius.setOnClickListener(radiusListener);
throwDart.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
throwDart.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
throwDart.setText("Throw Dart");
throwDart.setTextSize(10);
throwDart.setOnClickListener(throwListener);
numDarts = new EditText(this);
radius = new EditText(this);
radius.setHint("Set Radius");
radius.setText("3");
radius.setInputType(InputType.TYPE_CLASS_NUMBER);
numDarts.setText("5");
numDarts.setHint("Num Darts?");
numDarts.setInputType(InputType.TYPE_CLASS_NUMBER);
android.widget.FrameLayout.LayoutParams params = new android.widget.FrameLayout.LayoutParams(width, width);
board.setLayoutParams(params);
lin.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT , LinearLayout.LayoutParams.WRAP_CONTENT));
lin.setOrientation(LinearLayout.VERTICAL);
lin.addView(board);
linMain.addView(numDarts);
linMain.addView(throwDart);
linMain.addView(radius);
linMain.addView(setRadius);
lin.addView(linMain);
setContentView(lin);
}
View.OnClickListener throwListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
board.throwDarts(Integer.parseInt(numDarts.getText().toString()));
}
catch (NumberFormatException e){
Toast.makeText(getApplicationContext(), "Enter A number!", Toast.LENGTH_LONG).show();
}
}
};
View.OnClickListener radiusListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
int num = Integer.parseInt(radius.getText().toString());
board.changeRadius(num);
}
catch(NumberFormatException e){
Toast.makeText(getApplicationContext(), "Enter a Radius!", Toast.LENGTH_LONG).show();
}
}
};
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
board.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
board.resume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static class dartBoard extends SurfaceView implements Runnable{
Boolean threadRunning, throwingDarts;
SurfaceHolder holder;
Thread t = null;
Paint blue,red,black,white;
int rad, numberOfDarts;
Rect r;
int x[],y[];
public dartBoard(Context context) {
super(context);
// TODO Auto-generated constructor stub
threadRunning = false;
throwingDarts = false;
holder = getHolder();
blue = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Style.FILL);
red = new Paint();
red.setColor(Color.RED);
red.setStyle(Style.FILL);
black = new Paint();
black.setColor(Color.BLACK);
black.setStyle(Style.STROKE);
white = new Paint();
white.setColor(Color.WHITE);
white.setStyle(Style.FILL);
rad = 3;
}
public void onDraw(Canvas canvas){
canvas.getWidth();
r = new Rect(0,0, canvas.getWidth(), canvas.getWidth());
canvas.drawRect(r, blue);
canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, rad*(canvas.getWidth()/20) , red);
canvas.drawLine(0, canvas.getWidth()/2, canvas.getWidth(), canvas.getWidth()/2, black);
canvas.drawLine(canvas.getWidth()/2, 0, canvas.getWidth()/2, canvas.getWidth(), black);
if(throwingDarts){
for(int i = 0; i < numberOfDarts; i++){
canvas.drawCircle(x[i]*(canvas.getWidth()/20), y[i]*(canvas.getWidth()/20), (canvas.getWidth()/20), white);
canvas.drawCircle(x[i]*(canvas.getWidth()/20), y[i]*(canvas.getWidth()/20), (canvas.getWidth()/20), black);
}
}
}
@SuppressLint("WrongCall")
@Override
public void run() {
// TODO Auto-generated method stub
while(threadRunning){
if(!holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
onDraw(c);
holder.unlockCanvasAndPost(c);
}
}
public final void resume(){
t = new Thread(this);
t.start();
threadRunning = true;
}
public final void pause(){
while(true){
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
t = null;
}
public void changeRadius(int r){
rad = r;
}
public void throwDarts(int numDarts)
{
numberOfDarts = numDarts;
Random rand = new Random();
x = new int[numDarts];
y = new int[numDarts];
for(int i = 0; i < numDarts; i++)
{
x[i] = rand.nextInt(21);
y[i] = rand.nextInt(21);
}
throwingDarts = true;
}
public boolean hit(int x, int y)
{
if(Math.sqrt(x*x + y*y) <= 3)
{
return true;
}
else
{
return false;
}
}
}
}
这个应用没有什么特别的,只需在按下按钮时绘制到画布上。返回应用程序时,除了FC之外,它工作正常。谁能帮我?我很感激:]