Я пытаюсь изучить Android, и я делаю простое упражнение, включающее две кнопки и текстовое представление. Однако, когда я пытаюсь запустить приложение в эмуляторе, приложение принудительно закрывается.
Ниже приведен код:
public class CambiarColorActivity extends Activity
implements View.OnClickListener {
Button btnRed;
Button btnBlue;
TextView text;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView text = (TextView)findViewById(R.id.textView1);
btnRed=(Button)findViewById(R.id.button1);
btnBlue=(Button)findViewById(R.id.button2);
btnRed.setOnClickListener(this);
btnBlue.setOnClickListener(this);
}
public void onClick(View view) {
changeColor();
}
private void changeColor() {
if(btnRed.isPressed()) {
text.setBackgroundResource(Color.RED);
} else {
text.setBackgroundResource(Color.BLUE);
}
}
}
И вот ошибки, которые я нашел в журнале в Eclipse:
11-04 11:34:42.377: E/AndroidRuntime(376): Caused by: java.lang.NullPointerException
11-04 11:34:42.377: E/AndroidRuntime(376): at mi.entrenamiento.OrejanoX.CambiarColorActivity.onCreate(CambiarColorActivity.java:25)
Вот моя часть моего main.xml
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
style="@style/red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/red"
android:text="@string/red" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/blue"
android:text="@string/azul" />
</LinearLayout>
Любая помощь будет приветствоваться.
Спасибо и всего наилучшего, Мауро.