I am trying to figure out way this code isn't working.
The last line is giving me an warning
"Do not concentrate text displayed with set text".
Any help would be greatly appreciated.
public class RegistrationActivity extends AppCompatActivity {
double costPerAthlete=725.00;
int numberOfAthletes;
double totalCost;
String locationChoice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
final EditText tickets = (EditText) findViewById(R.id.txtTickets);
final Spinner location = (Spinner) findViewById(R.id.txtLocation);
Button cost = (Button) findViewById(R.id.btnCost);
cost.setOnClickListener(new View.OnClickListener() {
final TextView result = ((TextView) findViewById(R.id.txtResult));
@Override
public void onClick(View view) {
numberOfAthletes = Integer.parseInt(tickets.getText( ).toString( ));
totalCost = costPerAthlete * numberOfAthletes;
DecimalFormat currency = new DecimalFormat("$###,###.###");
locationChoice = location.getSelectedItem( ).toString( );
result.setText("Cost for" + locationChoice + "is" + currency.format(totalCost));
}
});
}
}
XML File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.matt.triathlon.RegistrationActivity"
tools:showIn="@layout/activity_registration">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtTitle"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="32sp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:hint="@string/txtTickets"
android:textSize="22sp"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtLocation"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:spinnerMode="dialog"
android:entries="@array/txtLocation"
android:focusableInTouchMode="false"
android:prompt="@string/prompt" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/triathalon"
android:src="@drawable/triathalon"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:contentDescription="@string/description" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnCost"
android:id="@+id/button"
android:textSize="28sp"
android:layout_below="@+id/triathalon"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:id="@+id/txtResult"
android:textSize="22sp" />
</RelativeLayout>
These are my logs
E/AndroidRuntime: FATAL EXCEPTION:
main Process: com.matt.triathlon, PID: 2188 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matt.triathlon/com.matt.triathlon.RegistrationActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)