【问题标题】:Incompatible types error in EditText androidEditText android中的不兼容类型错误
【发布时间】:2015-05-01 13:18:44
【问题描述】:

我的 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;
 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();
    }
}

}

【问题讨论】:

  • 你不能复制粘贴这么多代码并指望别人解决,请告诉你卡在哪里

标签: android text edit


【解决方案1】:

您必须将findViewById() 方法转换为您的View 类型,因为它会从处理过的XML 中找到由id 属性标识的视图。因此您必须在您的EditText查看。

试试这个:

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

【讨论】:

    【解决方案2】:

    你必须输入 cast findviewbyid

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

    【讨论】:

      【解决方案3】:

      您获得了 NPE,因为您的 player1null

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

      您是说player1 在上一个活动中。

      如果 player1 不在 activity_startwith2player 布局中,您将始终获得 NPE。

      如果你在之前的 Activity 中有这个值,你可以在你的 Intent 中使用 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

      顺便说一句,我建议你在课堂上使用大写字母。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-23
        • 2011-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多