Android app crashes when dialog box is called

877 views Asked by At

I'm still learning how to make android apps and I'm practicing using the different controls, like EditText, radio buttons, dialog boxes, etc.

I'm trying to have the user enter some info into the app and then display that info into a dialog box when they press the "save" button. Whenever I press save, the app crashes.

I know it has something to do with my onClick method where I set all the input to strings so they can be written to the dialog box. It seems to happening when I try to convert the data from the spinner, radio button, and toggle button to string. When I comment those out, the dialog box works fine with just the edit text data.

Can anyone see what's wrong?

Java code

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ToggleButton;
import android.widget.Spinner;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.content.DialogInterface;
import android.app.AlertDialog;
import java.text.DecimalFormat;
import java.util.Calendar;
public class ActMain extends Activity {


    //----------------------------------------------------------------
    // Variables
    //----------------------------------------------------------------

    // Declare variables
    EditText etAppName;
    Spinner spCategory;
    RadioGroup rgRating;
    RadioButton rbGood;
    RadioButton rbFair;
    RadioButton rbBad;
    ToggleButton tbGooglePlay;
    EditText etPrice;
    Button btnSave;

    //----------------------------------------------------------------
    // Activity overrides
    //----------------------------------------------------------------

    //----------------------------------------------------------------
    // onCreate
    //----------------------------------------------------------------

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("### DEBUG ### onCreate started at " + currentTime() + ".");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.laymain);

        // Define edit text controls
        etAppName = (EditText) findViewById(R.id.etAppName);
        etPrice = (EditText) findViewById(R.id.etPrice);

        // Define spinner adapter
        String[] categories = {"Business", "Comics", "Education", "Finance", "Games", "Music", "News", "Tools", "Travel", "Weather"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // Define spinner control
        spCategory = (Spinner) findViewById(R.id.spCategory);
        spCategory.setAdapter(adapter);

        // Define spinner event
        spCategory.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> parent, 
                    View view, int position, long id) 
            {
                System.out.println("Spinner: \"" + 
                        parent.getItemAtPosition(position) + 
                        "\" selected.");
            }
            public void onNothingSelected(AdapterView<?> parent)
            {
                System.out.println("Spinner: no item selected.");
            }
        });

        // Define save button click event
        btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle("Mobile App Reviewer Message");

                String appName = etAppName.getText().toString();
                String price = etPrice.getText().toString();
                String category = spCategory.getSelectedItem().toString();
                int rbCheckedId = rgRating.getCheckedRadioButtonId();
                RadioButton rbChecked = (RadioButton) findViewById(rbCheckedId);
                String rating = rbChecked.getText().toString();

                String onGooglePlay;
                if(tbGooglePlay.isChecked())
                    onGooglePlay = "NO";
                else
                    onGooglePlay = "YES";

                builder.setMessage("Application: " + appName +
                                   "\nCategory: " + category+ 
                                   "\nRating: " + rating + 
                                   "\nOn Google Play? " + onGooglePlay + 
                                   "\nPrice: $" + price + 
                                   "\nSave this data?");
                builder.show();

            }

        });
    }

XML 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="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <!--
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    Title rows - Text View
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    -->

    <TextView 
        android:text="Software News"
        android:textSize="22sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="0dp"
        android:layout_margin="4dp"/>         

    <TextView 
        android:text="Mobile App Reviewer"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="0dp"
        android:layout_margin="4dp"/> 

    <!--
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    App info
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    -->

    <!--
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    App name - Edit text
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    -->

    <LinearLayout 
        android:gravity="left" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Application: "
            android:textStyle="bold"
            android:textSize="14sp" 
            android:gravity="left" 
            android:layout_gravity="top" 
            android:padding="4dp" 
            android:layout_margin="4dp" />

        <EditText 
            android:id="@+id/etAppName"
            android:textSize="16sp"
            android:background="#33CCCC"
            android:gravity="left"
            android:layout_gravity="left"
            android:layout_width="100dp"
            android:layout_height="25dp"
            android:padding="0dp"
            android:layout_margin="4dp" />

    </LinearLayout>

    <!--
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    App Category - Spinner
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    -->

    <LinearLayout 
        android:gravity="left" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_margin="6dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Category: "
            android:textStyle="bold"
            android:textSize="14sp" 
            android:gravity="left" 
            android:layout_gravity="top" 
            android:padding="4dp" 
            android:layout_margin="4dp" />

        <Spinner 
            android:id="@+id/spCategory"
            android:layout_width="160dp"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <!--
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    App Rating - Radio Buttons
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    -->

    <LinearLayout 
        android:gravity="left" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_margin="6dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rating: "
            android:textStyle="bold"
            android:textSize="14sp" 
            android:gravity="left" 
            android:layout_gravity="top" 
            android:padding="4dp" 
            android:layout_margin="4dp" />

        <RadioGroup 
            android:id="@+id/rgRating"
            android:gravity="left" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:orientation="vertical" >

            <RadioButton 
                android:id="@+id/rbGood"
                android:text="Good"
                android:textSize="14sp"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:onClick="onRadioButtonClicked" />

            <RadioButton 
                android:id="@+id/rbFair"
                android:text="Fair"
                android:textSize="14sp"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:onClick="onRadioButtonClicked" />

            <RadioButton 
                android:id="@+id/rbBad"
                android:text="Bad"
                android:textSize="14sp"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:onClick="onRadioButtonClicked" />          
        </RadioGroup>
    </LinearLayout>    

    <!--
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    On Google Play? - Toggle Button
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    -->

    <LinearLayout 
        android:gravity="left" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_margin="6dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="On Google Play? "
            android:textStyle="bold"
            android:textSize="14sp" 
            android:gravity="left" 
            android:layout_gravity="top" 
            android:padding="4dp" 
            android:layout_margin="4dp" />

        <ToggleButton
            android:id="@+id/tbGooglePlay"
            android:textOn="NO"
            android:textOff="YES"
            android:textSize="12sp"
            android:textStyle="bold"
            android:layout_width="75dp"
            android:layout_height="wrap_content"/>
    </LinearLayout>         

    <!--
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    On Google Play? - Toggle Button
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    -->

    <LinearLayout 
        android:gravity="left" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_margin="6dp" >

        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Price: $ "
            android:textStyle="bold"
            android:textSize="14sp" 
            android:gravity="left" 
            android:layout_gravity="top" 
            android:padding="4dp" 
            android:layout_margin="4dp" />

        <EditText 
            android:id="@+id/etPrice"
            android:textSize="16sp"
            android:background="#33CCCC"
            android:inputType="numberDecimal"
            android:gravity="left"
            android:layout_gravity="left"
            android:layout_width="100dp"
            android:layout_height="25dp"
            android:padding="0dp"
            android:layout_margin="4dp" />

    </LinearLayout>

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:background="#A0CFEC"
        android:gravity="center"
        android:padding="4dp"
        android:text="Save"
        android:textSize="14sp"
        android:textStyle="bold" />

</LinearLayout>
4

There are 4 answers

2
DKIT On BEST ANSWER

rgRating is not assigned to anything. So you will get a nullPointerException when calling getCheckedRadioButtonId.

This would be obvious if you checked the stack trace or debugged the application.

To fix it, assign an ID to rgRating in the XML file and do a

rgRating = findViewById(...)

...before you try to call getCheckedRadioButtonId()

0
kandroidj On

Try this:

  AlertDialog dialog = builder.create();
  dialog.show();
0
ImStillPondering On

So after @DKIT pointed out my error with rgRating, I noticed that I had done the same thing with tbGooglePlay.

In the end, adding these lines of code to the Java file fixed my issue:

rgRating = (RadioGroup) findViewById(R.id.rgRating);
tbGooglePlay = (ToggleButton) findViewById(R.id.tbGooglePlay);
1
Xjasz On

change this

AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

to

AlertDialog.Builder builder = new AlertDialog.Builder(ActMain.this);