Reputation: 1470
I know this is a very simple question for you experts,but please forgive me.Im very new to this android platform.
I tried to display a string in a textview when a button is clicked.
But it is not working.No errors are showing.
Can you please help me with where im going wrong.
Thanks in advace.
Below is the code i wrote:
package com.mycmpny.namebtn;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
public class NameonbuttonclickActivity extends Activity implements View.OnClickListener {
Button mybtn;
TextView txtView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mybtn= new Button(this);
mybtn.setOnClickListener(this);
printmyname();
setContentView(mybtn);
// setContentView(R.layout.main);
}
public void onClick(View view){
printmyname();
}
private void printmyname(){
txtView.setText("This is my first app");
}
}
Upvotes: 0
Views: 820
Reputation: 18499
I have done some modifications in onCreate().here i am making a textview which you left.Do that and say is it working ?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mybtn= new Button(this);
mybtn.setOnClickListener(this);
txtView=new TextView(this);
// printmyname();
LinearLayout ll=new LinearLayOut(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(mybtn);
ll.addView(txtView);
setContentView(ll);
// setContentView(R.layout.main);
}
HTH :)
Upvotes: 1
Reputation: 111
You have not created a new object of TextView
.
txtView = new TextView(this);
Else use findViewById
if you've created the text View in your xml files.
txtView = (TextView)findViewById(R.id.TextView1);
Upvotes: 1