0

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)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • try see the value for locaitonChoice and currency.format(totalCost) before you setText – thatOneGuy Feb 24 '16 at 16:00
  • Been a while since I've worked with java, but System.println(locationChoice); System.println( currency.format(totalCost)); see what happens if you do that before you setText. I say this as I would console.log() if i were using javascript, so I'm not 100% that is the correct way, but may be worth checking out – thatOneGuy Feb 24 '16 at 16:06
  • the println is coming up red saying it can't resolve the method –  Feb 24 '16 at 16:15
  • which method is that ? I put System.println() i meant System.out.println(myObject); – thatOneGuy Feb 24 '16 at 16:20
  • Nah that didn't work. –  Feb 24 '16 at 17:04
  • it wants me to change the "+" symbols –  Feb 24 '16 at 17:05
  • It is a warning, not an error. You can safely ignore it – OneCricketeer Feb 24 '16 at 17:07
  • ok, so what do i have to do to get this to work? –  Feb 24 '16 at 17:15
  • every time i try to use the app in the emulator it crashes –  Feb 24 '16 at 17:15
  • Then post the logcat here – OneCricketeer Feb 24 '16 at 17:16
  • 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) at android.app.ActivityThread.-wrap11(Activity –  Feb 24 '16 at 17:48
  • `findViewById` will return `null` if the ID that you gave it cannot be found in the contentView set by `activity_registration.xml`. Please check if you have a Button with the ID of `btnCost` in that file. – OneCricketeer Feb 24 '16 at 17:51
  • I do have the button btnCost –  Feb 24 '16 at 17:53
  • Can you please [edit your question](http://stackoverflow.com/q/35606870/2308683) to include the XML layout file? – OneCricketeer Feb 24 '16 at 17:55
  • did'nt you get a compilation error for the code you paster?? because you dont have any id as btnCost. thats a string type resource. :) – srv_sud Feb 24 '16 at 18:14

2 Answers2

2

Your ID for the button in the layout is android:id="@+id/button", not android:id="@+id/btnCost".

That is why you are getting a NullPointerException.

Please either change the XML to use the correct ID, or change the Java code to

Button cost = (Button) findViewById(R.id.button);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Change

Button cost = (Button) findViewById(R.id.btnCost);

to

Button cost = (Button) findViewById(R.id.button);

It's always better to make youre own xmls and follow a convention for ids I use tvName for (tv_name), btnLogin for (btn_login) etc so that i dont get confused.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
yUdoDis
  • 1,098
  • 6
  • 15