can u tell me why this code is not working? The app stops as soon as I click on Image(Imageview1). Debugger points to tv.setText(x);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView tv = (TextView)findViewById(R.id.textView1);
setContentView(R.layout.activity_main);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String x="You clicked on the image.";
tv.setText(x);
}
});
}
You need to get your UI elements after inflating your layout, otherwise findViewById returns null and hence the NPE is thrown when you try to set the text on your textView.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); /// I swap these two lines
final TextView tv = (TextView)findViewById(R.id.textView1); ///
ImageView img = (ImageView) findViewById(R.id.imageView1);
Related
I'm a beginner, I did a simple task but I do not see the result in TextView. How can I solve my problem?
Code:
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText1, editText2;
TextView textView;
public void Add(View v)
{
float x = Float.parseFloat(editText1.getText().toString());
float y = Float.parseFloat(editText2.getText().toString());
float sum = x + y;
textView.setText(String.valueOf(sum));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText1 = findViewById(R.id.editText);
editText2 = findViewById(R.id.editText2);
textView = findViewById(R.id.textView);
}
}
The second option I tried was:
textView.setText("SUM = " + String.valueOf(sum));
But then I receive a message:
Do not concatenate text displayed with setText. Use resource string with placeholders.
PS. Of course i added method android:onClick = "Add"
You can try sum.toString() instead of String.valueOf(sum)
This will do your work
textView.setText(Float.toString(sum))
you better make button onclick in java, then display the string value to the textview
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleButtonClick();
}
private void handleButtonClick() {
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addAndSetResult();
}
});
}
private void addAndSetResult() {
// inside here do the add logic and set result to textview
EditText editText1 = (EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
float x = Float.parseFloat(editText1.getText().toString());
float y = Float.parseFloat(editText2.getText().toString());
float sum = x + y;
String sumStr = String.valueOf(sum);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(sumStr);
}
I'm testing a small animation example on Android studio. On the last method 'toggle' I'm getting an error Cannot resolve symbol views. Can someone help me to fix this issue?
public class MainActivity extends AppCompatActivity {
private ViewGroup viewGroup;
private TextView textView;
private ImageView imageView, imageView2, imageView3,imageView4,imageView7;
private Button button;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewGroup=(ViewGroup) findViewById(R.id.viewGroup);
textView= (TextView) findViewById(R.id.textView);
textView.setText("It's animating");
imageView= (ImageView) findViewById(R.id.imageView);
imageView2= (ImageView) findViewById(R.id.imageView2);
imageView3= (ImageView) findViewById(R.id.imageView3);
imageView4= (ImageView) findViewById(R.id.imageView4);
imageView7= (ImageView) findViewById(R.id.imageView7);
button = (Button)findViewById(R.id.clickme);
button.setText("Done");
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
TransitionManager.beginDelayedTransition(viewGroup, new Explode());
toggle(textView, imageView, imageView2, imageView3, imageView4, imageView7);
}
});
private static void toggle(View... views) {
for (View v : views) {
boolean isVisible = v.getVisibility() == View.VISIBLE;
v.setVisibility(isVisible ? View.INVISIBLE :
View.VISIBLE);
}
}
}}
I am trying to get the text in the EditText component named textfield and see if it equals "facebook". It does not work though and I can't for the life of me find out why. Also in the if statement my is supposed to be the ID for the picture.
public class Guess extends Activity {
ImageView image;
ImageView stat;
Button okButton;
EditText textfield;
Integer my;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guess);
final Bundle bundle = this.getIntent().getExtras();
int pic = bundle.getInt("myimage");
final int resid = bundle.getInt("resourceID");
my = pic;
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(pic);
image.setAdjustViewBounds(true);
stat = (ImageView) findViewById(R.id.imageView2);
stat.setImageResource(R.drawable.incorrect);
okButton = (Button) findViewById(R.id.button1);
okButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textfield = (EditText) findViewById(R.id.editText1);
if("2130837510".equals(my.toString()) && "facebook".equals(textfield.getText()));
{
stat.setImageResource(R.drawable.correct);
}
}
});
}
}
textfield.getText() returns an Editable not a String. To make it work pass the actual String object using textfield.getText().toString().
You have a semi colon on the end of your if statement
if(1 == 2);
{
System.out.println("passed");
}
so the code below will execute anyway.
I have an android program that I am making with 4 seekbars, and 4 textviews to accompany each seekbar. Right now, I am trying to make each textview's contents equal to the integer progress of the seekbars. This is my code as it stands, and I have tried many ways to properly implement the OnSeekBarChangeListener, but LogCat still shows that it is null when sb1-sb4 sets their OnSeekBarChangeListener to OnSeekBarProgress. Please help me identify my problem :(
public class TippopotamusActivity extends Activity {
SeekBar sb1;
SeekBar sb2;
SeekBar sb3;
SeekBar sb4;
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
sb1 = (SeekBar) findViewById(R.id.seekBar1);
sb2 = (SeekBar) findViewById(R.id.seekBar2);
sb3 = (SeekBar) findViewById(R.id.seekBar3);
sb4 = (SeekBar) findViewById(R.id.seekBar4);
sb1.setOnSeekBarChangeListener(OnSeekBarProgress);
sb2.setOnSeekBarChangeListener(OnSeekBarProgress);
sb3.setOnSeekBarChangeListener(OnSeekBarProgress);
sb4.setOnSeekBarChangeListener(OnSeekBarProgress);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
OnSeekBarChangeListener OnSeekBarProgress =
new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar s, int progress, boolean touch){
if(s.getId() == R.id.seekBar1)
{
tv1.setText(progress);
}
else if(s.getId() == R.id.seekBar2)
{
tv2.setText(progress);
}
else if(s.getId() == R.id.seekBar3)
{
tv3.setText(progress);
}
else
{
tv4.setText(progress);
}
}
public void onStartTrackingTouch(SeekBar s){
}
public void onStopTrackingTouch(SeekBar s){
}
};
Move your setContentView(R.layout.main); before initializing your variables. Your onCreate method should look like this
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
sb1 = (SeekBar) findViewById(R.id.seekBar1);
sb2 = (SeekBar) findViewById(R.id.seekBar2);
sb3 = (SeekBar) findViewById(R.id.seekBar3);
sb4 = (SeekBar) findViewById(R.id.seekBar4);
sb1.setOnSeekBarChangeListener(OnSeekBarProgress);
sb2.setOnSeekBarChangeListener(OnSeekBarProgress);
sb3.setOnSeekBarChangeListener(OnSeekBarProgress);
sb4.setOnSeekBarChangeListener(OnSeekBarProgress);
}
For some reason my application crashes when I try to set the text of a textview? How can I over come this.
private TextView strStatus;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txtUsername = (EditText) findViewById(R.id.username);
strStatus = (TextView) findViewById(R.id.status);
//strStatus.setText("test"); // SEEMS TO BE CAUSING THE CRASH
setContentView(R.layout.main);
}
<TextView
android:id="#+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
One small mistake , You should do that setContentView first
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtUsername = (EditText) findViewById(R.id.username);
strStatus = (TextView) findViewById(R.id.status);
strStatus.setText("test");
}