2015-05-01 150 views
-3

我的Java代碼如下,我得到java.lang.NullPointerException從這一行:錯誤類型

OnCreate方法
firstplayer = player1.getText().toString; 

,其中player1EditText距上一次活動的一個。

我該如何解決?

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.EditText; 


public class startwith2player extends ActionBarActivity { 

private boolean playerone = true , playertwo = false ; 
private Integer score1 = 0 , score2 = 0; 
private Integer flag4 = 0 , flag5 = 0 , flag6 = 0 , flag7 = 0, flag8 = 0, flag9 = 0 , flag10 = 0, flag11 = 0, flag12 = 0; 
private Integer counter1 = 0 , counter2 = 0; 
    TextView tex1 ; 
    TextView tex2 ; 
String firstplayer = ""; 
String secondplayer = ""; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_startwith2player); 
    tex1 = (TextView) findViewById(R.id.textView3); 
    tex2 = (TextView) findViewById(R.id.textView4); 
    EditText player1 = (EditText)findViewById(R.id.editText); 
    EditText player2 = (EditText)findViewById(R.id.editText2); 
    firstplayer = player1.getText().toString(); 
    secondplayer = player2.getText().toString(); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_startwith2player, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
public void refresh(){ 
    BlueAllBoxes(); 
    if(playerone){ 
     playerone = false; 
     playertwo = true; 
    } 
    else{ 
    playerone = true; 
    playertwo = false; 
    } 
    flag4 = 0 ; flag5 = 0 ; flag6 = 0 ; flag7 = 0; flag8 = 0; flag9 = 0 ; flag10 = 0; flag11 = 0; flag12 = 0; 
    counter1 = 0 ; counter2 = 0; 
} 

public void BlueAllBoxes(){ 
    Button button4 = (Button) findViewById(R.id.button4); 
    button4.setBackgroundColor(0xFF4B4BFF); 
    Button button5 = (Button) findViewById(R.id.button5); 
    button5.setBackgroundColor(0xFF4B4BFF); 
    Button button6 = (Button) findViewById(R.id.button6); 
    button6.setBackgroundColor(0xFF4B4BFF); 
    Button button7 = (Button) findViewById(R.id.button7); 
    button7.setBackgroundColor(0xFF4B4BFF); 
    Button button8 = (Button) findViewById(R.id.button8); 
    button8.setBackgroundColor(0xFF4B4BFF); 
    Button button9 = (Button) findViewById(R.id.button9); 
    button9.setBackgroundColor(0xFF4B4BFF); 
    Button button10 = (Button) findViewById(R.id.button10); 
    button10.setBackgroundColor(0xFF4B4BFF); 
    Button button11 = (Button) findViewById(R.id.button11); 
    button11.setBackgroundColor(0xFF4B4BFF); 
    Button button12 = (Button) findViewById(R.id.button12); 
    button12.setBackgroundColor(0xFF4B4BFF); 
} 
public void button4(View view) { 
    if(counter1 == 3){ 
     if(flag4 == 1) { 
      view.setBackgroundColor(0xFF67FF5A); 
      counter2++; 

      if(playerone) { 
       score1 = score1 + 30; 
       // tex1.setText(String.valueOf(score1)); 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 + 30; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
      if(counter2 == 3) 
      refresh(); 
     } 
     else{ 
      view.setBackgroundColor(0xFFFF231E); 
      if(playerone) { 
       score1 = score1 - 10; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 - 10; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
     } 
    } 
else { 
     if (flag4 == 0) { 
      flag4 = 1; 
      view.setBackgroundColor(0xFF67FF5A); 
      counter1++; 
      if(counter1 == 3) 
       BlueAllBoxes(); 
     } 
    } 
} 
public void button5(View view) { 
    if(counter1 == 3){ 
     if(flag5 == 1) { 
      view.setBackgroundColor(0xFF67FF5A); 
      counter2++; 

       if(playerone) { 
        score1 = score1 + 30; 
        tex1.setText(firstplayer + " : " + score1); 
       } 
       else { 
        score2 = score2 + 30; 
        tex2.setText(secondplayer + " : " + score2); 
       } 
       if(counter2 == 3) 
       refresh(); 
     } 
     else{ 
      view.setBackgroundColor(0xFFFF231E); 
      if(playerone) { 
       score1 = score1 - 10; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 - 10; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
     } 
    } 
    else { 
     if (flag5 == 0) { 
      flag5 = 1; 
      view.setBackgroundColor(0xFF67FF5A); 
      counter1++; 
      if(counter1 == 3) 
       BlueAllBoxes(); 
     } 
    } 
} 
public void button6(View view) { 
    if(counter1 == 3){ 
     if(flag6 == 1) { 
      view.setBackgroundColor(0xFF67FF5A); 
      counter2++; 

      if(playerone) { 
       score1 = score1 + 30; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 + 30; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
       if(counter2 == 3) 
       refresh(); 
     } 
     else{ 
      view.setBackgroundColor(0xFFFF231E); 
      if(playerone) { 
       score1 = score1 - 10; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 - 10; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
     } 
    } 
    else { 
     if (flag6 == 0) { 
      flag6 = 1; 
      view.setBackgroundColor(0xFF67FF5A); 
      counter1++; 
      if(counter1 == 3) 
       BlueAllBoxes(); 
     } 
    } 
} 
public void button7(View view) { 
    if(counter1 == 3){ 
     if(flag7 == 1) { 
      view.setBackgroundColor(0xFF67FF5A); 
      counter2++; 

      if(playerone) { 
       score1 = score1 + 30; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 + 30; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
      if(counter2 == 3) 
       refresh(); 
     } 
     else{ 
      view.setBackgroundColor(0xFFFF231E); 
      if(playerone) { 
       score1 = score1 - 10; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 - 10; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
     } 
    } 
    else { 
     flag7 = 1; 
     view.setBackgroundColor(0xFF67FF5A); 
     counter1++; 
     if(counter1 == 3) 
      BlueAllBoxes(); 
    } 
} 
public void button8(View view) { 
    if(counter1 == 3){ 
     if(flag8 == 1) { 
      view.setBackgroundColor(0xFF67FF5A); 
      counter2++; 

      if(playerone) { 
       score1 = score1 + 30; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 + 30; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
      if(counter2 == 3) 
       refresh(); 
     } 
     else{ 
      view.setBackgroundColor(0xFFFF231E); 
      if(playerone) { 
       score1 = score1 - 10; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 - 10; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
     } 
    } 
    else { 
     flag8 = 1; 
     view.setBackgroundColor(0xFF67FF5A); 
     counter1++; 
     if(counter1 == 3) 
      BlueAllBoxes(); 
    } 
} 
public void button9(View view) { 
    if(counter1 == 3){ 
     if(flag9 == 1) { 
      view.setBackgroundColor(0xFF67FF5A); 
      counter2++; 

      if(playerone) { 
       score1 = score1 + 30; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 + 30; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
      if(counter2 == 3) 
       refresh(); 
     } 
     else{ 
      view.setBackgroundColor(0xFFFF231E); 
      if(playerone) { 
       score1 = score1 - 10; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 - 10; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
     } 
    } 
    else { 
     flag9 = 1; 
     view.setBackgroundColor(0xFF67FF5A); 
     counter1++; 
     if(counter1 == 3) 
      BlueAllBoxes(); 
    } 
} 
public void button10(View view) { 
    if(counter1 == 3){ 
     if(flag10 == 1) { 
      view.setBackgroundColor(0xFF67FF5A); 
      counter2++; 

      if(playerone) { 
       score1 = score1 + 30; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 + 30; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
      if(counter2 == 3) 
       refresh(); 
     } 
     else{ 
      view.setBackgroundColor(0xFFFF231E); 
      if(playerone) { 
       score1 = score1 - 10; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 - 10; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
     } 
    } 
    else { 
     flag10 = 1; 
     view.setBackgroundColor(0xFF67FF5A); 
     counter1++; 
     if(counter1 == 3) 
      BlueAllBoxes(); 
    } 
} 
public void button11(View view) { 
    if(counter1 == 3){ 
     if(flag11 == 1) { 
      view.setBackgroundColor(0xFF67FF5A); 
      counter2++; 

      if(playerone) { 
       score1 = score1 + 30; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 + 30; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
      if(counter2 == 3) 
       refresh(); 
     } 
     else{ 
      view.setBackgroundColor(0xFFFF231E); 
      if(playerone) { 
       score1 = score1 - 10; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 - 10; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
     } 
    } 
    else { 
     flag11 = 1; 
     view.setBackgroundColor(0xFF67FF5A); 
     counter1++; 
     if(counter1 == 3) 
      BlueAllBoxes(); 
    } 
} 
public void button12(View view) { 
    if(counter1 == 3){ 
     if(flag12 == 1) { 
      view.setBackgroundColor(0xFF67FF5A); 
      counter2++; 

      if(playerone) { 
       score1 = score1 + 30; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 + 30; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
      if(counter2 == 3) 
       refresh(); 
     } 
     else{ 
      view.setBackgroundColor(0xFFFF231E); 
      if(playerone) { 
       score1 = score1 - 10; 
       tex1.setText(firstplayer + " : " + score1); 
      } 
      else { 
       score2 = score2 - 10; 
       tex2.setText(secondplayer + " : " + score2); 
      } 
     } 
    } 
    else { 
     flag12 = 1; 
     view.setBackgroundColor(0xFF67FF5A); 
     counter1++; 
     if(counter1 == 3) 
      BlueAllBoxes(); 
    } 
} 

} 
+0

您無法複製和粘貼大量的代碼並期望其他人解決它,請告訴您卡在哪裏 –

回答

0

你要投findViewById()方法您View類型,因爲它認爲是確定的從在處理XMLid屬性的看法。所以,你必須強制類型轉換查看到您的EditText視圖。

試試這個:

EditText player1 = (EditText)findViewById(R.id.editText); 
EditText playertwo = (EditText)findViewById(R.id.editText2); 
0

,你必須強制類型轉換findviewbyid

EditText player1 = (EditText)findViewById(R.id.editText); 
EditText playertwo = (EditText)findViewById(R.id.editText2); 
0

你得到一個NPE,因爲你的player1null

EditText player1 = (EditText)findViewById(R.id.editText); 
firstplayer = player1.getText().toString(); 

你是說,player1在之前的Activi中TY。

如果player1不在activity_startwith2player佈局中,您將永遠得到一個NPE。

如果您在之前的活動中擁有此值,則可以在您的意圖中使用Extra來共享此值。

Intent intent = new Intent(this, startwith2player.class); 
    EditText player1 = (EditText)findViewById(R.id.editText); 
    String message = player1.getText().toString(); 
    intent.putExtra(EXTRA_MESSAGE, message); 

更多信息here

順便說一下,我建議你爲你的班級使用大寫字母。