为什么我从EditText android获得NullPointerException?

时间:2015-04-27 14:01:45

标签: android text nullpointerexception android-edittext set

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;
EditText tex = (EditText) findViewById(R.id.editText3);
@Override
protected void onCreate(Bundle savedInstanceState) {
   // setContentView(R.layout.activity_startwith2player);

    System.out.println("yes yes");
   // StartByPlayerOne();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startwith2player);
   // StartByPlayerOne();
   // prrrint();
  //  Bundle extras = getIntent().getExtras();
//    Integer latLong = extras.getInt("NameOfPlayer1");
  //  TextView player = (TextView)findViewById(R.id.textView3);
 //   player.setText("" + latLong);
 //   System.out.println(latLong);
}

我想使用tex.SetText(String)但在此之前我从第三行得到了NullPointerException :( 任何的想法 ?!?!         机器人:的onClick = “button12”/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="score1"
    android:id="@+id/textView3"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText3"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/button4"
    android:layout_toEndOf="@+id/button4"
    android:text="AA" />

那是我的XML代码

3 个答案:

答案 0 :(得分:1)

tex = (EditText) findViewById(R.id.editText3);必须在setContentView(R.layout.activity_startwith2player);

之后

答案 1 :(得分:1)

试试这个:

str

答案 2 :(得分: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;
EditText tex;
@Override
protected void onCreate(Bundle savedInstanceState) {
    System.out.println("yes yes");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startwith2player);

    tex = (EditText) findViewById(R.id.editText3);

}