调用对话框时,Android应用程序崩溃

时间:2015-06-08 16:28:34

标签: java android alertdialog

我还在学习如何制作Android应用程序,我正在练习使用不同的控件,如EditText,单选按钮,对话框等。

我正在尝试让用户在应用程序中输入一些信息,然后在按下“保存”按钮时将该信息显示在对话框中。每当我按保存时,应用程序崩溃。

我知道它与我的onClick方法有关,我将所有输入设置为字符串,以便将它们写入对话框。当我尝试将数据从微调器,单选按钮和切换按钮转换为字符串时,似乎发生了这种情况。当我对它们进行注释时,只需编辑文本数据,对话框就可以正常工作。

有人能看出什么问题吗?

Java代码

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代码

<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 个答案:

答案 0 :(得分:0)

试试这个:

  AlertDialog dialog = builder.create();
  dialog.show();

答案 1 :(得分:0)

更改此

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

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

答案 2 :(得分:0)

rgRating未分配给任何内容。所以在调用getCheckedRadioButtonId时会得到nullPointerException。

如果您检查了堆栈跟踪或调试了应用程序,这将是显而易见的。

要修复它,请在XML文件中为rgRating分配一个ID并执行

rgRating = findViewById(...)

...在您尝试调用getCheckedRadioButtonId()

之前

答案 3 :(得分:0)

所以在@DKIT用rgRating指出我的错误之后,我注意到我用tbGooglePlay做了同样的事情。

最后,将这些代码行添加到Java文件中解决了我的问题:

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