因此,对于我的第一个Android应用程序,我认为我只会对计算机做一个简单的猜数游戏。在完成了几个教程并将我的eclipse独立版本重新调整为有希望成为可运行的应用程序之后。但是,每次我按下我的猜测'按钮,我收到以下错误:
12-24 23:53:11.470 823-823/cartersperkins.whatsthatnumber D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
12-24 23:53:13.300 823-823/cartersperkins.whatsthatnumber I/Choreographer﹕ Skipped 111 frames! The application may be doing too much work on its main thread.
12-24 23:53:14.350 823-823/cartersperkins.whatsthatnumber I/Choreographer﹕ Skipped 372 frames! The application may be doing too much work on its main thread.
12-24 23:53:14.830 823-823/cartersperkins.whatsthatnumber D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
12-24 23:53:18.040 823-823/cartersperkins.whatsthatnumber I/Choreographer﹕ Skipped 266 frames! The application may be doing too much work on its main thread.
12-24 23:53:20.850 823-823/cartersperkins.whatsthatnumber V/EditText﹕ 500+
12-24 23:53:37.990 823-823/cartersperkins.whatsthatnumber I/Choreographer﹕ Skipped 59 frames! The application may be doing too much work on its main thread.
12-24 23:53:55.115 823-823/cartersperkins.whatsthatnumber V/EditText﹕ 750-
12-24 23:53:55.175 823-823/cartersperkins.whatsthatnumber I/System.out﹕ 0/0
12-24 23:53:59.005 823-823/cartersperkins.whatsthatnumber I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread.
我尝试了this问题下的一些评论,但我真的不明白他们想说什么。这是我的应用程序代码:
package cartersperkins.whatsthatnumber;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
import java.util.Scanner;
public class Game extends Activity {
Button mButton;
EditText mEdit;
private int targetNumber;
public int lastMin, lastMax;
public int turn;
public String abc;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.hide();
setContentView(R.layout.activity_game);
mButton = (Button) findViewById(R.id.guessButton);
mEdit = (EditText) findViewById(R.id.targetGuess);
mText = (TextView) findViewById(R.id.targetRange);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.v("EditText", mEdit.getText().toString());
gameStart();
clicked = true;
xyz = mEdit.getText().toString();
}
});
}
public void gameStart() {
Random rand = new Random();
if(game) {
turn = 0;
game = true;
lastMin = 0;
lastMax = 1000;
targetNumber = rand.nextInt(1001);
System.out.println("The number is between 0-1000.");
game = false;
}
Scanner scan = new Scanner(System.in);
// System.out.println(targetNumber);
// while (game)
//
// {
if (clicked) {
range(xyz);
int a, b;
if ((lastMax - lastMin) <= 100
&& (lastMax - lastMin) > 10) {
a = rand.nextInt(lastMin + (rand.nextInt(25) + 26));
b = rand.nextInt(lastMax - (rand.nextInt(25) + 26));
} else if ((lastMax - lastMin) <= 500
&& (lastMax - lastMin) > 100) {
a = rand.nextInt(lastMin + (rand.nextInt(100) + 101));
b = rand.nextInt(lastMax + (rand.nextInt(100) + 101));
} else if ((lastMax - lastMin) > 500) {
a = rand.nextInt(lastMin + (rand.nextInt(100) + 151));
b = rand.nextInt(lastMax + (rand.nextInt(100) + 151));
} else if ((lastMax - lastMin) <= 5
&& (lastMax - lastMin) > 2) {
a = (lastMin) + (rand.nextInt(3) + 1);
b = (lastMax) - (rand.nextInt(3) + 1);
} else if ((lastMax - lastMin) == 2) {
if (lastMax < targetNumber
&& lastMin < targetNumber) {
a = (lastMin + 1);
b = (lastMax + 1);
} else if (lastMax > targetNumber
&& lastMin > targetNumber) {
a = (lastMin - 1);
b = (lastMax - 1);
} else {
a = (lastMin + 1);
b = (lastMax - 1);
}
} else if ((lastMax - lastMin) == 0) {
a = lastMax;
b = lastMin;
} else {
a = rand.nextInt(1000) + 1;
b = rand.nextInt(1000) + 1;
}
String y = a + "/" + b;
System.out.println(y);
range(y);
clicked = false;
}
// }
}
public String input(TextView z) {
return z.toString();
}
public void range(String guess) {
if (clicked) {
int guessMin = 0, guessMax = 0;
if (guess.contains("+")) {
guessMin = Integer.parseInt(guess.substring(0, guess.indexOf('+')));
guessMax = lastMax;
} else if (guess.contains("-")) {
guessMax = Integer.parseInt(guess.substring(0, guess.indexOf('-')));
guessMin = lastMin;
} else if (guess.contains("/")) {
guessMin = Integer.parseInt(guess.substring(0, guess.indexOf('/')));
guessMax = Integer
.parseInt(guess.substring(guess.indexOf('/') + 1));
} else {
guessMin = Integer.parseInt(guess);
guessMax = Integer.parseInt(guess);
}
turn++;
if (guessMin == targetNumber && guessMax == targetNumber) {
if (turn % 2 == 0)
System.out.println("Computer Won!");
else
System.out.println("Player Won!");
game = false;
} else {
if (guessMin > lastMin && guessMin <= targetNumber)
lastMin = guessMin;
if (guessMax < lastMax && guessMax >= targetNumber)
lastMax = guessMax;
if (game) {
System.out.println("The number is between " + lastMin + "-"
+ lastMax + ".");
abc = lastMin + "-"
+ lastMax;
mText.setText(abc);
}
}
clicked = false;
}
}
public String getAbc() {
return abc;
}
public void setAbc(String abc) {
this.abc = abc;
}
public boolean clicked = false;
public boolean isClicked() {
return clicked;
}
public void setClicked(boolean clicked) {
this.clicked = clicked;
}
public String y;
private boolean game;
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
public String xyz;
public TextView mText;
public String getXyz() {
return xyz;
}
public void setXyz(String xyz) {
this.xyz = xyz;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_game, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
感谢任何帮助或帮助链接。
答案 0 :(得分:4)
尝试使用AsyncTask请参阅此链接
http://developer.android.com/reference/android/os/AsyncTask.html
private class MyAsyncTask extends AsyncTask<Object , Object , Object > {
protected Object doInBackground(Object... urls) {
// do some thing in background
return result;
}
@Override
protected void onPreExecute() {
// this will execute on main thread before Method doInBackground()
super.onPreExecute();
}
protected void onPostExecute(Object result) {
// this will execute on main thread after Method doInBackground()
}
}
要使用此new MyAsyncTask().execute("");
如果您有任何疑问,请问我。
希望这会有所帮助:)
答案 1 :(得分:2)
你必须使用Asynctask。
private class DownloadMp3Task extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
//Yet to code
}
protected void onProgressUpdate(Integer... progress) {
//Yet to code
}
protected void onPostExecute(Long result) {
//Yet to code
}
}
答案 2 :(得分:0)
事情不是那个!它只是警告不是错误的!发布logcat,你得到FATAL EXCEPTION。你可以在主线程上执行此操作只有你不能的网络。你可能有另一个问题只是在logcat中查找FATAL EXCEPTION!
答案 3 :(得分:0)
顺便说一句,这些不是错误消息。这些是警告,我已经看到这些警告出现在使用OpenGL进行一些UI工作的应用程序中,但是当您使用模拟器时更是如此。尝试在Android手机中运行您的应用程序以调查日志。它们可能出现也可能不出现。
使用AsyncTask或Threads执行密集型作业,以便它不会挂起主线程。当您在UI线程中进行密集工作时,您的应用程序基本上不会响应。但是,这并不能保证您不会看到这些警告。有些是在绘制时由操作系统本身生成的,只要应用程序运行正常,就可以忽略。