Reputation: 250
I'm trying to make a very simple Android app based off of the first tutorial on the Android Developers site, and I have hit a snag. It's probably something really stupid, but I have code that compiles and looks good to me but doesn't do what it's supposed to. Here's my MainActivity Java. It's supposed to display your weight on Mars after you put in your weight on Earth. However, when I push my "Send" button, nothing happens.
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
EditText editText = (EditText) findViewById(R.id.editText1);
int weight = Integer.parseInt(editText.getText().toString());
double fweight = mweight(weight);
String finalmessage = Double.toString(fweight);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(finalmessage);
}
public double mweight(int eweight) {
double mass, mars_g = 3.711;
mass = eweight / 9.780327;
return mass * mars_g;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Thanks in advance!
Upvotes: 1
Views: 1160
Reputation: 86948
You need to attach your new TextView to the current layout with View#addView()
or switch the entire layout to your TextView with Activity#setContentView()
:
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(finalmessage);
setContentView(textView); // Add me!
To be clear this assumes that you have used android:onClick="sendMessage"
in your Button's XML.
Upvotes: 3