I’m new to Android. I’ve download some code to run but there are problems:
package t.t.t4;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Test4Activity extends Activity {
/** Called when the activity is first created. */
OnClickListener listener1 = null;
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener1 = new OnClickListener() {
public void onClick(View v) {
TextView text_view = (TextView) findViewById(R.id.TextView01);
CharSequence text_view_old = text_view.getText();
text_view.setText("Before: "+ text_view_old +"\nAdded information: Hello World again !");
}
};
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.Button01);
button1.setOnClickListener(listener1);
}
}
R.id.TextView01 is the line that had an error. What is the meaning of R and R.id?
You need to add
setContentView(R.layout.main);after thesuper.onCreate();because all the widgets are declared in the layout file so first it needs to be loaded. Heretextviewis declared in themain.xmlfile.That is why it is giving the error, becuase you use that textview before it sets the view.
you have to declare
texview with id TextView01in yourmain.xmlfile.