1
package com.example.prova;

import java.util.Enumeration;
import java.util.Hashtable;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ScassaLaTalpa extends Activity 
         implements android.view.View.OnClickListener, 
                    TextWatcher {

    int life = 3;
    public static int NUMBER_OF_CELLS = 16;
    Hashtable<Integer, Button> buttons = 
                    new Hashtable<Integer, Button>();
    TextView lose;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        buttons = populateGrid(buttons);
        Enumeration<Integer> keys = buttons.keys();

        while(keys.hasMoreElements()){
         Integer key = (Integer) keys.nextElement();
            Button btn = buttons.get(key);
            btn.setText("");
            btn.setOnClickListener(this);
        }

      lose = (TextView) findViewById(R.id.lose);
      lose.addTextChangedListener(this);
      lose.setText(life + "");
      SpawnButton sb = new SpawnButton();
      sb.execute();
   }

   class SpawnButton extends AsyncTask<Void, Button, String> {

      @Override
      protected String doInBackground(Void...voids) {
         Button randomBtn= null;

         while(life > 0){
            int randomBtnNumber = (int) (Math.random() * 16);
            randomBtn= buttons.get(randomBtnNumber );
            publishProgress(randomBtn);
            try {
               Thread.sleep(1500);
            } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
    publishProgress(randomBtn);      
         }
         return "";
      }


      @Override
      protected void onProgressUpdate(Button... values) {
         // TODO Auto-generated method stub
     if(values[0].getText().equals("")
            values[0].setText("X");
     else
    values[0].setText("");
      }

      @Override
      protected void onPostExecute(String result) {
         // TODO Auto-generated method stub
         TextView lose = (TextView) findViewById(R.id.lose);
         lose.setText("You lose!");

         Enumeration<Integer> keys = buttons.keys();

         while(keys.hasMoreElements()){
            Integer key = (Integer) keys.nextElement();
            Button btn = buttons.get(key);
            btn.setBackgroundColor(Color.BLACK);
            btn.setEnabled(false);
         }   
      }
   }

   public void onClick(View v) {
      // TODO Auto-generated method stub
      Button btn= (Button) v;
      if(btn.getText().equals("X")){   // ??
         btn.setText("");
      } else {
         life--;
         lose.setText(life + "");
      }
   }

   /* UTILS METHOD */

   public Hashtable<Integer, Button> populateGrid(
                                        Hashtable<Integer, 
                                        Button> buttons){

      buttons.put(1, (Button) findViewById(R.id.btn1));
      buttons.put(2, (Button) findViewById(R.id.btn2));
      buttons.put(3, (Button) findViewById(R.id.btn3));
      buttons.put(4, (Button) findViewById(R.id.btn4));
      buttons.put(5, (Button) findViewById(R.id.btn5));
      buttons.put(6, (Button) findViewById(R.id.btn6));
      buttons.put(7, (Button) findViewById(R.id.btn7));
      buttons.put(8, (Button) findViewById(R.id.btn8));
      buttons.put(9, (Button) findViewById(R.id.btn9));
      buttons.put(10, (Button) findViewById(R.id.btn10));
      buttons.put(11, (Button) findViewById(R.id.btn11));
      buttons.put(12, (Button) findViewById(R.id.btn12));
      buttons.put(13, (Button) findViewById(R.id.btn13));
      buttons.put(14, (Button) findViewById(R.id.btn14));
      buttons.put(15, (Button) findViewById(R.id.btn15));
      buttons.put(16, (Button) findViewById(R.id.btn16));

      return buttons;
   }

   public void afterTextChanged(Editable s) {
      // TODO Auto-generated method stub

      if(s.toString().equals("0"))
         lose.setTextColor(Color.RED);
      else if(s.toString().equals("1"))
         lose.setTextColor(Color.MAGENTA);
      else if(s.toString().equals("2"))
         lose.setTextColor(Color.YELLOW);
      else if(s.toString().equals("3"))
         lose.setTextColor(Color.GREEN);

   }

   public void beforeTextChanged(CharSequence s,
                                 int start, 
                                 int count,
                                 int after) {
      // TODO Auto-generated method stub

   }

   public void onTextChanged(CharSequence s, 
                             int start, 
                             int before, 
                             int count) {
      // TODO Auto-generated method stub

   }
}

我的目标是创建一个按钮网格并将按钮文本随机设置为“X”。我想模拟“鼹鼠猎人”游戏。因此,当按下激活的按钮时,文本将设置为“”以取消激活它。当按下未激活的按钮时,生命将减一。等等......这段代码有效,但它经常崩溃......我没有使用好的线程方法?

12-18 21:20:01.500: E/AndroidRuntime(1270): FATAL EXCEPTION: main

12-18 21:20:01.500: E/AndroidRuntime(1270): java.lang.NullPointerException

12-18 21:20:01.500: E/AndroidRuntime(1270):     at     com.example.pwnthamarmot.ScassaLaTalpa$SpawnMarmot.onProgressUpdate(ScassaLaTalpa.java:80)

12-18 21:20:01.500: E/AndroidRuntime(1270):     at com.example.pwnthamarmot.ScassaLaTalpa$SpawnMarmot.onProgressUpdate(ScassaLaTalpa.java:1)

12-18 21:20:01.500: E/AndroidRuntime(1270):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432)

12-18 21:20:01.500: E/AndroidRuntime(1270):     at android.os.Handler.dispatchMessage(Handler.java:99)

12-18 21:20:01.500: E/AndroidRuntime(1270):     at android.os.Looper.loop(Looper.java:130)

12-18 21:20:01.500: E/AndroidRuntime(1270):     at android.app.ActivityThread.main(ActivityThread.java:3687)

12-18 21:20:01.500: E/AndroidRuntime(1270):     at java.lang.reflect.Method.invokeNative(Native Method)

12-18 21:20:01.500: E/AndroidRuntime(1270):     at java.lang.reflect.Method.invoke(Method.java:507)

12-18 21:20:01.500: E/AndroidRuntime(1270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)

12-18 21:20:01.500: E/AndroidRuntime(1270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)

12-18 21:20:01.500: E/AndroidRuntime(1270):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

如果您仔细查看堆栈跟踪,您会发现您进入NullPointerException了文件的第 80 行ScassaLaTalpa.java,特别是在您的ScassaLaTalpa$SpawnMarmot.onProgressUpdate方法中。没有看到该文件(标明行号),我不能说更多。

于 2012-12-18T20:35:09.390 回答