displayTextView.setText ("cambio en el código"); ¿Está Mal?

A simple android Hello World:

MainActivity.java:

package com.amaker.ch02.app;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    private TextView displayTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        displayTextView = (TextView)findViewById(R.id.DisplayTextView);
        displayTextView.setText("change in the code");    <--Right if delete the line
    }
}

res / layout / main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id = "@+id/DisplayTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

Run, and android will display: sorry, the application has stopped unexpectedly. Please try again., but just as I point in the code MainActivity.java, if I delete the code displayTextView.setText("change in the code");, todo está bien.

What's wrong with the line of code?

preguntado el 01 de febrero de 12 a las 03:02

does the textView show up without that line? Does it display the default text...? Also, try deleting the private modifier for the textView. -

also, by convention, I believe variable names are supposed to be lowercase and then camelCase. -

Oh, right, i have modified from "DisplayTextView" to "displayTextView", convention is also important. Thanks @Kgrover -

But without "private", also "stopped unexpectedly"...@Kgrover -

Your code is working fine... try to rebuild -

1 Respuestas

your code seems right. try to clean and build the project. also try to change textview id in xml as well as in your code.your DisplayTextView might coming null so creating this problem. debug the code and find the actual cause. or provide us logcat details.

Respondido 01 Feb 12, 07:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.