android programming help

FalconSS

[H]ard|Gawd
Joined
Jul 2, 2004
Messages
1,297
Hey guys,
I decided to try my hand at android programming. I have limited experience in C++, HTML, and javascript.

I am basically making a "hello world" app that takes two numbers and adds them together when a button is pressed. In the future this app will be converted to calculate the least common multiple and greatest common divisor. It will also have three tabs with one being the calculater, one being an explanation of how to do the work, and one being an "about" page.
Thats going to be a long way off and this is my getting to know android project and it will evolve as I learn more.

currently I can run the app on the virtual machine in eclipse. but when I press the "calculate button the app closes. I cant figure out where I went wrong so any help will be appreciated. (or point me to someone who can).

This is my .java file

Code:
package least.common.multiple;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;



public class Main extends Activity {

	 int numberThree = 0;
	 int numberOne = 0;
	 int numberTwo = 0;
	 EditText tt;
	 EditText editnumberOne;
	 EditText editnumberTwo;
	 TextView textView;


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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void button(View view) {
    	//do something in response to the button
   	 try {
		    numberOne = Integer.parseInt(editnumberOne.getText().toString());
		} catch(NumberFormatException nfe) {
		   System.out.println("Could not parse " + nfe);
		} 
	 
		 try {
			    numberTwo = Integer.parseInt(editnumberTwo.getText().toString());
			} catch(NumberFormatException nfa) {
			   System.out.println("Could not parse " + nfa);
			} 
    	
      
    	numberThree = numberOne + numberTwo;
    	
        
        textView.setText(String.valueOf(numberThree));
    	

    }
}


and this is my XML file

Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom"
    android:orientation="vertical" >

    <EditText
        android:id= "@+id/editnumberOne"
        android:layout_width="156dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/number_one"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <EditText
        android:id= "@+id/editnumberTwo"
        android:layout_width="156dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/number_two"
        android:inputType="number" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="button"
        android:text="@string/button_send" />
  
    <TextView
        android:id="@+id/textView"
        android:layout_width="156dp"
        android:layout_height="20dp"
        android:layout_gravity="center" />
    
</LinearLayout>
 
I could be totally wrong, since I have limited Java experience (no android), but your program is probably executing and then closing immediately after. In other words, I'm thinking you might need a "pause" or use something like .hasNext() from the Scanner class or something similar becuase the output is so quick. I'm sure someone with experience will get this cleared up for you; sorry I'm little help.
 
the interesting thing is that I can get (parts of) it to work if I use an Intent and output to a new activity. I basically used the beginning tutorial from developers.android.com and made changes as I went to help me learn what things did. that particular tutorial has one text field that you enter in text to, it then opens up a new activity and outputs your message when you enter a button.

on converting it, I learned about the parse function that lets me get integers from a string. (so I can work with them). im not sure if there is a problem with that or like you say, I have no pause function included.
 
Back
Top