我正在使用 AppWarp-SDK 为云和游戏管理服务编写应用程序。但是,我收到一个错误,无法弄清楚原因。
当我单击按钮时,startClick
会调用该方法,并且在调试器中出现以下错误。谁能发现问题?我试图自己弄清楚,但经过 2 小时的尝试后无法解决。我知道这是太多代码,但我想提供所有必要的信息。
07-12 18:53:32.206: I/ActivityManager(89): Displayed pack.BAAppWarp42/.GameActivity: +2s383ms
07-12 18:54:08.720: D/AndroidRuntime(1664): Shutting down VM
07-12 18:54:08.720: W/dalvikvm(1664): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-12 18:54:12.796: E/AndroidRuntime(1664): FATAL EXCEPTION: main
07-12 18:54:12.796: E/AndroidRuntime(1664): java.lang.IllegalStateException: Could not execute method of the activity
07-12 18:54:12.796: E/AndroidRuntime(1664): at android.view.View$1.onClick(View.java:3044)
07-12 18:54:12.796: E/AndroidRuntime(1664): at android.view.View.performClick(View.java:3511)
07-12 18:54:12.796: E/AndroidRuntime(1664): at android.view.View$PerformClick.run(View.java:14105)
07-12 18:54:12.796: E/AndroidRuntime(1664): at android.os.Handler.handleCallback(Handler.java:605)
07-12 18:54:12.796: E/AndroidRuntime(1664): at android.os.Handler.dispatchMessage(Handler.java:92)
07-12 18:54:12.796: E/AndroidRuntime(1664): at android.os.Looper.loop(Looper.java:137)
07-12 18:54:12.796: E/AndroidRuntime(1664): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-12 18:54:12.796: E/AndroidRuntime(1664): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 18:54:12.796: E/AndroidRuntime(1664): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 18:54:12.796: E/AndroidRuntime(1664): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-12 18:54:12.796: E/AndroidRuntime(1664): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-12 18:54:12.796: E/AndroidRuntime(1664): at dalvik.system.NativeStart.main(Native Method)
07-12 18:54:12.796: E/AndroidRuntime(1664): Caused by: java.lang.reflect.InvocationTargetException
07-12 18:54:12.796: E/AndroidRuntime(1664): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 18:54:12.796: E/AndroidRuntime(1664): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 18:54:12.796: E/AndroidRuntime(1664): at android.view.View$1.onClick(View.java:3039)
07-12 18:54:12.796: E/AndroidRuntime(1664): ... 11 more
07-12 18:54:12.796: E/AndroidRuntime(1664): Caused by: java.lang.NullPointerException
07-12 18:54:12.796: E/AndroidRuntime(1664): at pack.BAAppWarp42.GameActivity.startClick(GameActivity.java:195)
07-12 18:54:12.796: E/AndroidRuntime(1664): ... 14 more
07-12 18:54:12.916: W/ActivityManager(89): Force finishing activity pack.BAAppWarp42/.GameActivity
有我的源代码。请忽略其余部分,有很多评论我必须尽快清理。
package pack.BAAppWarp42;
import java.util.Hashtable;
import java.util.Timer;
import org.json.JSONException;
import org.json.JSONObject;
import com.shephertz.app42.gaming.multiplayer.client.command.WarpResponseResultCode;
import com.shephertz.app42.gaming.multiplayer.client.events.ChatEvent;
import com.shephertz.app42.gaming.multiplayer.client.events.LiveRoomInfoEvent;
import com.shephertz.app42.gaming.multiplayer.client.events.LobbyData;
import com.shephertz.app42.gaming.multiplayer.client.events.MoveEvent;
import com.shephertz.app42.gaming.multiplayer.client.events.RoomData;
import com.shephertz.app42.gaming.multiplayer.client.events.RoomEvent;
import com.shephertz.app42.gaming.multiplayer.client.events.UpdateEvent;
import com.shephertz.app42.gaming.multiplayer.client.listener.NotifyListener;
import com.shephertz.app42.gaming.multiplayer.client.listener.RoomRequestListener;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class GameActivity extends Activity implements RoomRequestListener, NotifyListener{
//Variables
boolean playing = true;
boolean online = false;
int colorGenerator;
int chosenColor;
int targetedColor;
int startColor;
static int[][] colorArray;
static int[][] colorArrayTest;
String colorString;
long reactionTime;
static long start;
static long end;
Thread changer;
Timer timer = new Timer();
int counter = 0;
long lastTimeStamp = 0;
ColorChanger colorChanger = new ColorChanger(this);
View thisView;
long latencySent;
long latencyRespons;
long latency;
private int selectedCell = Constants.INVALID_SELECTION;
private Handler UIThreadHandler;
private String gameRoomId;
private JSONObject gameObject;
private String localUserName;
private String remoteUserName;
private int localUserCellImageId;
private char localUserTile;
private String currentState;
private String nextTurn;
private ProgressDialog progressDialog;
private ImageButton selectedButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
colorArray = new int[3][10];
Intent intent = getIntent();
localUserName = intent.getStringExtra(Constants.IntentUserName);
gameRoomId = intent.getStringExtra(Constants.IntentGameRoomId);
AsyncApp42ServiceApi.getMyWarpClient().addRoomRequestListener(this);
AsyncApp42ServiceApi.getMyWarpClient().addNotificationListener(this);
AsyncApp42ServiceApi.getMyWarpClient().subscribeRoom(gameRoomId);
}
@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 void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public void multiplayerJoinClick (View view) throws InterruptedException{
}
public static JSONObject buildNewGameJSON(String username){
// prepare the gameObj myself as I'm the one who joined first.
JSONObject gameObject = new JSONObject();
try {
gameObject.put(Constants.GameFirstUserKey, username);
gameObject.put(Constants.GameSecondUserKey, "");
gameObject.put(Constants.GameStateKey, Constants.GameStateIdle);
gameObject.put(Constants.GameBoardKey, Constants.GameEmptyBoard);
gameObject.put(Constants.GameWinnerKey, "");
gameObject.put(Constants.GameNextMoveKey, username);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return gameObject;
}
public void startClick(View view){
try {
String boardState = gameObject.getString(Constants.GameBoardKey);
boardState += "0";
gameObject.put(Constants.GameBoardKey, boardState);
AsyncApp42ServiceApi.getMyWarpClient().setCustomRoomData(gameRoomId, gameObject.toString());
AsyncApp42ServiceApi.getMyWarpClient().sendUpdatePeers(Constants.UpdateTrigger);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// colorArray = giveColorArray();
//
// setContentView(R.layout.game);
// findViewById(R.id.buttonLeft).setVisibility(View.VISIBLE);
// findViewById(R.id.buttonMid).setVisibility(View.VISIBLE);
// findViewById(R.id.buttonRight).setVisibility(View.VISIBLE);
// findViewById(R.id.winLoseTextfield).setVisibility(View.VISIBLE);
// findViewById(R.id.targetButton).setVisibility(View.VISIBLE);
//
// findViewById(R.id.targetButton).setBackgroundColor(targetedColor);
// findViewById(R.id.targetButton).setClickable(false);
// findViewById(R.id.resetButton).setClickable(false);
//
// if(targetedColor == Color.BLACK || targetedColor == Color.BLUE){
// Button targetButton = (Button)findViewById(R.id.targetButton);
// targetButton.setTextColor(Color.WHITE);
// }
//
// Play();
}
public void multiplayerGenerateClick(View view){
colorArray = giveColorArray();
colorString = colorArrayToString(colorArray);
}
private void updateUI()
{
try {
//this.findViewById(R.id.accountButton).setVisibility(View.INVISIBLE);
String u1Name = gameObject.getString(Constants.GameFirstUserKey);
String u2Name = gameObject.getString(Constants.GameSecondUserKey);
String winner = gameObject.getString(Constants.GameWinnerKey);
currentState = gameObject.getString(Constants.GameStateKey);
nextTurn = gameObject.getString(Constants.GameNextMoveKey);
if(u1Name.equals(localUserName) && u2Name.isEmpty()){
progressDialog = ProgressDialog.show(this, "", "waiting for opponent");
}
if(u1Name.equalsIgnoreCase(localUserName))
{
// localUserCellImageId = R.drawable.cross_cell;
// localUserTile = Constants.BoardTileCross;
// remoteUserName = u2Name;
}
else
{
// localUserCellImageId = R.drawable.circle_cell;
// localUserTile = Constants.BoardTileCircle;
// remoteUserName = u1Name;
}
//((Button) this.findViewById(R.id.submit)).setClickable(false);
// this.drawImagesForBoard(gameObject.getString(Constants.GameBoardKey));
if(currentState.equals(Constants.GameStateFinished))
{
if(u2Name.equalsIgnoreCase(localUserName)){
// ((Button) this.findViewById(R.id.rematch)).setVisibility(View.VISIBLE);
}
//((Button) this.findViewById(R.id.submit)).setVisibility(View.INVISIBLE);
if(winner.isEmpty())
{
// ((TextView) this.findViewById(R.id.status)).setText("Match Drawn!");
}
else if(winner.equalsIgnoreCase(localUserName))
{
// ((TextView) this.findViewById(R.id.status)).setText("You Won!");
}
else
{
// ((TextView) this.findViewById(R.id.status)).setText("You Lost :(");
}
}
else
{
// ((Button) this.findViewById(R.id.rematch)).setVisibility(View.INVISIBLE);
// ((TextView) this.findViewById(R.id.status)).setText("");
if(nextTurn.equalsIgnoreCase(localUserName))
{
// ((TextView) this.findViewById(R.id.status)).setText("Your turn against " + remoteUserName);
//((Button) this.findViewById(R.id.submit)).setVisibility(View.VISIBLE);
}
else
{
// ((TextView) this.findViewById(R.id.status)).setText("Waiting for "+remoteUserName+" to move");
//((Button) this.findViewById(R.id.submit)).setVisibility(View.INVISIBLE);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void startMultiplayerGame(View view) throws InterruptedException{
setContentView(R.layout.game);
findViewById(R.id.buttonLeft).setVisibility(View.VISIBLE);
findViewById(R.id.buttonMid).setVisibility(View.VISIBLE);
findViewById(R.id.buttonRight).setVisibility(View.VISIBLE);
findViewById(R.id.winLoseTextfield).setVisibility(View.VISIBLE);
findViewById(R.id.targetButton).setVisibility(View.VISIBLE);
findViewById(R.id.targetButton).setBackgroundColor(targetedColor);
findViewById(R.id.targetButton).setClickable(false);
findViewById(R.id.resetButton).setClickable(false);
if(targetedColor == Color.BLACK || targetedColor == Color.BLUE){
Button targetButton = (Button)findViewById(R.id.targetButton);
targetButton.setTextColor(Color.WHITE);
}
Play();
}
private void Play() throws InterruptedException{
changer = new Thread(new ColorChanger(this));
changer.start();
}
@Override
protected void onPause(){
super.onPause();
// tell opponent that I am leaving the room
AsyncApp42ServiceApi.getMyWarpClient().leaveRoom(gameRoomId);
}
public void onRematchClicked(View view) {
gameObject = Utilities.buildNewGameJSON(localUserName);
try {
gameObject.put(Constants.GameSecondUserKey, remoteUserName);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AsyncApp42ServiceApi.getMyWarpClient().setCustomRoomData(gameRoomId, gameObject.toString());
AsyncApp42ServiceApi.getMyWarpClient().sendUpdatePeers(Constants.UpdateTrigger);
}
public void leftClick(View view){
if (ColorChanger.colorLeft == targetedColor){
end = System.currentTimeMillis();
gameWin(view);
}
else{
gameLose(view);
}
}
public void midClick(View view){
if (ColorChanger.colorMid == targetedColor){
end = System.currentTimeMillis();
gameWin(view);
}
else{
gameLose(view);
}
}
public void rightClick(View view){
if (ColorChanger.colorRight == targetedColor){
end = System.currentTimeMillis();
gameWin(view);
}
else{
gameLose(view);
}
}
private void gameWin(View view){
reactionTime = end-start;
ColorChanger.running = false;
TextView ergebnisfeld = (TextView)findViewById(R.id.winLoseTextfield);
ergebnisfeld.setText("Richtig gedrückt. Sehr gut. Reaktionszeit: "+reactionTime+"ms");
endGame();
}
public void gameLose(View view){
ColorChanger.running = false;
TextView ergebnisfeld = (TextView)findViewById(R.id.winLoseTextfield);
ergebnisfeld.setText("Falsch gedrückt. Sorry");
endGame();
}
public void endGame(){
findViewById(R.id.buttonRight).setClickable(false);
findViewById(R.id.buttonMid).setClickable(false);
findViewById(R.id.buttonLeft).setClickable(false);
findViewById(R.id.resetButton).setVisibility(View.VISIBLE);
findViewById(R.id.resetButton).setClickable(true);
}
public void resetClick(View view){
finish();
startActivity(getIntent());
}
public void cancelClick(View view){
findViewById(R.id.multiplayerButtonGenerate).setVisibility(View.VISIBLE);
findViewById(R.id.multiplayerButtonJoin).setVisibility(View.VISIBLE);
findViewById(R.id.gameButton).setVisibility(View.VISIBLE);
findViewById(R.id.cancelButton).setVisibility(View.INVISIBLE);
}
public void accountClick(View view){
}
public void swapButton(View view){
}
public void handleOpponentMove(long timestamp, String move){
if(findViewById(R.id.cancelButton).getVisibility() == 0){
findViewById(R.id.pingButton).setVisibility(View.INVISIBLE);
}
else{
findViewById(R.id.pingButton).setVisibility(View.VISIBLE);
}
}
public void startPing(View view){
if(!online)
return;
}
private void sendPayload(String payload, long timeStamp)
{
latencySent = System.currentTimeMillis();
}
public int giveRandomColor(){
colorGenerator = (int) (Math.random()*7+1);
switch (colorGenerator){
case 1: chosenColor = Color.RED; break;
case 2: chosenColor = Color.BLUE; break;
case 3: chosenColor = Color.YELLOW; break;
case 4: chosenColor = Color.GREEN; break;
case 5: chosenColor = Color.MAGENTA; break;
case 6: chosenColor = Color.GRAY; break;
case 7: chosenColor = Color.BLACK; break;
default:chosenColor = targetedColor;
}
return chosenColor;
}
public int[][] giveColorArray(){
startColor = (int) (Math.random()*7+1);
switch (startColor){
case 1: targetedColor = Color.RED; break;
case 2: targetedColor = Color.BLUE; break;
case 3: targetedColor = Color.YELLOW; break;
case 4: targetedColor = Color.GREEN; break;
case 5: targetedColor = Color.MAGENTA; break;
case 6: targetedColor = Color.GRAY; break;
case 7: targetedColor = Color.BLACK; break;
default: targetedColor = Color.GREEN;
}
int[][] localColorArray = new int[3][10];
for(int i = 0; i < 10; i++){
localColorArray[0][i] = giveRandomColor();
do{
localColorArray[1][i] = giveRandomColor();
}
while(localColorArray[0][i] == localColorArray[1][i]);
do{
localColorArray[2][i] = giveRandomColor();
}
while(localColorArray[0][i] == localColorArray[2][i] || localColorArray[1][i] == localColorArray[2][i]);
}
return localColorArray;
}
public String colorArrayToString(int[][] array){
String result = String.valueOf(targetedColor);
for(int i = 0; i < 10; i++){
result = result + "," + array[0][i];
result = result + "," + array[1][i];
result = result + "," + array[2][i];
}
return result;
}
public int[][] colorStringToArray(String string){
String[] stringArray = string.split(",");
int[][] resultArray = new int[3][10];
int arrayCounter = 0;
targetedColor = Integer.parseInt(stringArray[0]);
for(int i = 1; i < stringArray.length; i = i + 3){
resultArray[0][arrayCounter] = Integer.parseInt(stringArray[i]);
resultArray[1][arrayCounter] = Integer.parseInt(stringArray[i+1]);
resultArray[2][arrayCounter] = Integer.parseInt(stringArray[i+2]);
arrayCounter++;
}
return resultArray;
}
@Override
public void onChatReceived(ChatEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void onMoveCompleted(MoveEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPrivateChatReceived(String arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onRoomCreated(RoomData arg0) {
// TODO Auto-generated method stub
}
@Override
public void onRoomDestroyed(RoomData arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpdatePeersReceived(UpdateEvent arg0) {
AsyncApp42ServiceApi.getMyWarpClient().getLiveRoomInfo(gameRoomId);
}
@Override
public void onUserChangeRoomProperty(RoomData arg0, String arg1,
Hashtable<String, Object> arg2, Hashtable<String, String> arg3) {
// TODO Auto-generated method stub
}
@Override
public void onUserJoinedLobby(LobbyData arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onUserJoinedRoom(final RoomData arg0, final String arg1) {
// callback is not the main UI thread. So post message to UI thread
// through its handler. Android UI elements can't be accessed from
// non-UI threads.
UIThreadHandler.post(new Runnable() {
@Override
public void run() {
if(progressDialog != null ){
progressDialog.dismiss();
}
remoteUserName = arg1;
// ((TextView) GameActivity.this.findViewById(R.id.status)).setText("Your turn against " + remoteUserName);
try {
gameObject.put(Constants.GameSecondUserKey, remoteUserName);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void onUserLeftLobby(LobbyData arg0, String arg1) {
// TODO Auto-generated method stub
}
@Override
public void onUserLeftRoom(final RoomData arg0, final String arg1) {
// oh looks like my opponent has left :-(
// callback is not the main UI thread. So post message to UI thread
// through its handler. Android UI elements can't be accessed from
// non-UI threads.
UIThreadHandler.post(new Runnable() {
@Override
public void run() {
if(arg1.equals(remoteUserName)){
gameObject = Utilities.buildNewGameJSON(localUserName);
AsyncApp42ServiceApi.getMyWarpClient().setCustomRoomData(gameRoomId, gameObject.toString());
GameActivity.this.updateUI();
}
}
});
}
@Override
public void onGetLiveRoomInfoDone(final LiveRoomInfoEvent arg0) {
if(arg0.getResult() != WarpResponseResultCode.SUCCESS){
return;
}
// callback is not the main UI thread. So post message to UI thread
// through its handler. Android UI elements can't be accessed from
// non-UI threads.
UIThreadHandler.post(new Runnable() {
@Override
public void run() {
try {
String customData = arg0.getCustomData();
String roomName = arg0.getData().getName();
((TextView) GameActivity.this.findViewById(R.id.room_info)).setText("Room: " + roomName);
if(customData == null || customData.length() == 0){
gameObject = Utilities.buildNewGameJSON(localUserName);
AsyncApp42ServiceApi.getMyWarpClient().setCustomRoomData(gameRoomId, gameObject.toString());
}
else{
gameObject = new JSONObject(arg0.getCustomData());
}
updateUI();
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void onJoinRoomDone(RoomEvent arg0) {
}
@Override
public void onLeaveRoomDone(RoomEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void onLockPropertiesDone(byte arg0) {
// TODO Auto-generated method stub
}
@Override
public void onSetCustomRoomDataDone(LiveRoomInfoEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void onSubscribeRoomDone(RoomEvent arg0) {
if(arg0.getResult() == WarpResponseResultCode.SUCCESS){
AsyncApp42ServiceApi.getMyWarpClient().getLiveRoomInfo(gameRoomId);
}
}
@Override
public void onUnSubscribeRoomDone(RoomEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUnlockPropertiesDone(byte arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpdatePropertyDone(LiveRoomInfoEvent arg0) {
// TODO Auto-generated method stub
}
}