2015-04-27 103 views
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 = (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(字符串),但在此之前,我得到的NullPointerException從第3線路:( 任何想法 安卓?!?!的onClick = 「button12」/>爲什麼我從EditText android得到NullPointerException?

<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代碼

+0

你能分享你的佈局文件? – TechnoBlahble

+0

是的,我分享 –

回答

1

tex = (EditText) findViewById(R.id.editText3);必須經過setContentView(R.layout.activity_startwith2player);

+0

好的,謝謝你,但如果我想使用「tex」出OnCreate方法,我必須將「void OnCreate」更改爲「EditText OnCreate」? –

1

試試這個:

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; 
private EditText tex ; 

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

} 
相關問題