EditText android中的不兼容类型错误
下面是我的java代码,我从这一行获得EditText android中的不兼容类型错误,android,text,edit,Android,Text,Edit,下面是我的java代码,我从这一行获得java.lang.NullPointerException: firstplayer = player1.getText().toString; 在OnCreate方法中,其中player1是先前活动中的EditText之一 我怎样才能修好它 import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; i
java.lang.NullPointerException
:
firstplayer = player1.getText().toString;
在OnCreate
方法中,其中player1
是先前活动中的EditText
之一
我怎样才能修好它
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();
}
}
}
您必须将
findViewById()
方法强制转换为视图
类型,因为它从中处理的XML
中查找由id
属性标识的视图。因此,您必须在EditText
视图中键入cast view
试试这个:
EditText player1 = (EditText)findViewById(R.id.editText);
EditText playertwo = (EditText)findViewById(R.id.editText2);
您必须键入cast findviewbyd
EditText player1 = (EditText)findViewById(R.id.editText);
EditText playertwo = (EditText)findViewById(R.id.editText2);
您获得NPE是因为您的
player1
为null
EditText player1 = (EditText)findViewById(R.id.editText);
firstplayer = player1.getText().toString();
您是说播放器1
在上一个活动中
如果player1
不在activity\u startwith2player
布局中,您将始终获得NPE
如果您在上一个活动中有此值,您可以在意图中使用额外的值来共享此值
Intent intent = new Intent(this, startwith2player.class);
EditText player1 = (EditText)findViewById(R.id.editText);
String message = player1.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
更多信息
顺便说一句,我建议你在课堂上使用大写字母。你不能复制和粘贴那么多代码,而指望别人来解决它,请告诉我你在哪里卡住了