2014-05-07 52 views
-1

我已經在這個空指針異常上搜索了幾個小時,但無濟於事。從我讀過的東西看來,似乎還有一些尚未實例化的對象。我一遍又一遍修改了我的代碼,但是我無法找到出錯的地方。如果我註釋掉包含compTurn()的for循環,在onCreate();那麼它會進入主屏幕,但顯然不會執行我所需要的功能。android java.lang.NullPointerException - 我想我已經實例化了每個對象

預先感謝您:)

package com.hunsdale.cognitive; 

import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.Random; 

import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class Main extends Activity { 

    // Initialise ArrayLists 
    static ArrayList<Integer> Computer; 
    static ArrayList<Integer> Player; 

    // Initialise Computer's Arraylist Iterator 
    static Iterator<Integer> CompIt; 

    // Initialise computer's pick and user's pick for arraylist 
    static int compPick, userPick, level; 

    // Initialise counter for score 
    static int counter; 

    // Initialise random number generator and boolean(for next level 
    // advance) 
    static Random rand; 

    // Initialise sounds for buttons 
    static MediaPlayer Mblue, Mgreen, Mred, Morange, Mturn; 

    // Initialise buttons 
    static ImageView blue, green, red, orange, turnSwitch; 

    // Initialise string to show their score to the user 
    TextView score; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // set content = main.xml 
     setContentView(R.layout.main); 

     // Instantiate buttons 
     blue = (ImageView) findViewById(R.id.blue_button); 
     blue.setOnClickListener(onClickListener); 

     green = (ImageView) findViewById(R.id.green_btn); 
     green.setOnClickListener(onClickListener); 

     red = (ImageView) findViewById(R.id.red_btn); 
     red.setOnClickListener(onClickListener); 

     orange = (ImageView) findViewById(R.id.orange_btn); 
     orange.setOnClickListener(onClickListener); 

     // Instantiate button sounds 
     Mblue = MediaPlayer.create(Main.this, R.raw.bubble); 
     Mgreen = MediaPlayer.create(Main.this, R.raw.release); 
     Mred = MediaPlayer.create(Main.this, R.raw.texture_scratch); 
     Morange = MediaPlayer.create(Main.this, R.raw.whistle_scratch); 

     // Instantiate score display 
     score = (TextView) findViewById(R.id.score); 

     // Instantiate ArrayLists 
     Computer = new ArrayList<Integer>(); 

     compPick = 0; 
     userPick = 0; 

     for (int i = 0; i <= 20; i++) { 

      compTurn(); 
     } 

    } 

    public static void compTurn() { 

     if (Player.equals(Computer)) { 

      level++; 

      Mblue.reset(); 
      Mgreen.reset(); 
      Mred.reset(); 
      Morange.reset(); 

      Player = new ArrayList<Integer>(); 

      counter = 0; 

      // the buttons cannot be clicked 
      blue.setEnabled(false); 
      green.setEnabled(false); 
      red.setEnabled(false); 
      orange.setEnabled(false); 

      compPick = rand.nextInt(4); 
      Computer.add(compPick); 


      for (Iterator<Integer> CompIt = Computer.iterator(); CompIt.hasNext();) { 
       Integer i = CompIt.next(); 

       if (i == 0) { 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        blue.setImageResource(R.drawable.btn_blue_hghlight); 
        Mblue.start(); 
        try { 
         Thread.sleep(500); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        blue.setImageResource(R.drawable.cst_btn_blue); 
        Mblue.reset(); 

       } else if (i == 1) { 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        green.setImageResource(R.drawable.btn_green_highlight); 
        Mgreen.start(); 
        try { 
         Thread.sleep(500); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        green.setImageResource(R.drawable.cst_btn_green); 
        Mgreen.reset(); 

       } else if (i == 2) { 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        red.setImageResource(R.drawable.btn_red_highlight); 
        Mred.start(); 
        try { 
         Thread.sleep(500); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        red.setImageResource(R.drawable.cst_btn_red); 
        Mred.reset(); 

       } else if (i == 3) { 
        try { 
         Thread.sleep(1000); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        orange.setImageResource(R.drawable.btn_orange_highlight); 
        Morange.start(); 
        try { 
         Thread.sleep(500); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
        orange.setImageResource(R.drawable.big_btn_orange2); 
        Morange.reset(); 

       } 
      } 
     } else if (counter < Computer.size()) { 
      enableUserTurn(true); 
      counter++; 
     } 

    } 

    public static void enableUserTurn(boolean buttonsEnabled) { 

     // enable buttons to be clicked 
     blue.setEnabled(buttonsEnabled); 
     green.setEnabled(buttonsEnabled); 
     red.setEnabled(buttonsEnabled); 
     orange.setEnabled(buttonsEnabled); 

    } 

    private OnClickListener onClickListener = new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      switch (v.getId()) { 
      case R.id.blue_button: 
       Mblue.start(); 
       Player.add(counter, 0); 
       if (Player.indexOf(counter) == (Computer.indexOf(counter))) { 

        compTurn(); 

       } else { 
        score.setText("Fail"); 
       } 
       break; 

      case R.id.green_btn: 
       Mgreen.start(); 
       Player.add(counter, 1); 
       if (Player.indexOf(counter) == (Computer.indexOf(counter))) { 

        compTurn(); 

       } else { 
        score.setText("Fail"); 
       } 
       break; 

      case R.id.red_btn: 
       Mred.start(); 
       Player.add(counter, 2); 
       if (Player.indexOf(counter) == (Computer.indexOf(counter))) { 

        compTurn(); 

       } else { 
        score.setText("Fail"); 
       } 

       break; 

      case R.id.orange_btn: 
       Morange.start(); 
       Player.add(counter, 3); 
       Morange.reset(); 
       if (Player.indexOf(counter) == (Computer.indexOf(counter))) { 

        compTurn(); 

       } else { 
        score.setText("Fail"); 
       } 

       break; 
      } 

     } 
    }; 

} 
+2

轉儲內容 –

+0

'了Thread.sleep(1000);在UI線程'必須拆除 – Raghunandan

+0

嘗試自己保持到Java約定:字段和方法以小寫字母,類以大寫字母開頭。這有助於可讀性。此外,不要保留靜態引用到你的'View's。這可能會導致內存泄漏。 – nhaarman

回答

1

罪魁禍首是這樣的:

if (Player.equals(Computer)) 

你錯過了創建對象Player,它爲空。如果將其更改爲

if (Computer.equals(Player)) 

它應該工作。

1

您正試圖在初始化之前使用Player

做到這一點:這裏的logcat的

// Instantiate ArrayLists 
     Computer = new ArrayList<Integer>(); 
     Player = new ArrayList<Integer>(); 

     compPick = 0; 
     userPick = 0; 

     for (int i = 0; i <= 20; i++) { 

      compTurn(); 
     } 
     ... 
相關問題