Я пытаюсь изучить Android, и я делаю простое упражнение с двумя кнопками и текстовым просмотром. Однако, когда я пытаюсь запустить приложение в эмуляторе, приложение принудительно отключается.Закрытие приложения при тестировании на AVD
Ниже приведен код:
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);
}
}
}
И эти ошибки, которые я нашел в журнале в Затмении:
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>
Любая помощь будет приветствоваться.
Спасибо и с уважением, Мауро.
Я добавил main.xml к исходному сообщению. Я получаю нулевое значение, но могу понять, почему. – Orejano
Есть несколько причин для этого; Google «findViewById возвращает null», и я уверен, что вы найдете его. – Jong