Good Day,
I am new to android and using Eclipse 3.7.2. I have been following examples on the net (the so-called “add two numbers” examples but, after discovering that AbsoluteLayout is deprecated, I am using RelativeLayout and basically going from scratch. The app basically is supposed to take two numbers (how far a guy went and how much he is paid per mile) and pop out total pay.
The app compiles fine and starts up in the emulator. I get excited! Pop in 2 numbers and hit the button. Nothing. I look and see that there’s no errors happening.
Am I missing something with my button listener? Here is the Java code I’ve put in (updated 5:27pm 10.31.2012):
package com.example.wing_it;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText mile,driver;
Button button1;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mile= (EditText) findViewById(R.id.mile);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.pay);
driver= (EditText) findViewById(R.id.driver);
button1.setOnClickListener((OnClickListener) this);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class clicker implements Button.OnClickListener
{
public void onClick(View v) {
// TODO Auto-generated method stub
{
String a,b;
Integer vis;
a = mile.getText().toString();
b = driver.getText().toString();
vis = Integer.parseInt(a)*Integer.parseInt(b);
tv.setText(vis.toString());
}
}
}
}
The XML code is as such:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/miles"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="40dp"
android:text="@string/paypermile"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginLeft="52dp"
android:layout_marginTop="62dp"
android:layout_toRightOf="@+id/textView2"
android:text="@string/compute" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="46dp"
android:text="@string/pay"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/mile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button1"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/driver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/mile"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/pay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignParentRight="true"
android:text="@string/totalpay"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Here is the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wing_it"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I have the value of the result textview set to nothing (just a blank) in the strings.xml. It was set to ‘0’ and I removed that to see if it would make a difference. Still, nothing.
Is there something I am overlooking here? Thank you for looking. 🙂
it’s hard to read your code, but this should work (sorry if i have spelling errors, i cannot cut/paste from my code (on another machine)