调用对话框时java Android应用程序崩溃
我仍在学习如何制作安卓应用程序,并在练习使用不同的控件,如编辑文本、单选按钮、对话框等
我试图让用户在应用程序中输入一些信息,然后在他们按下“保存”按钮时将这些信息显示在对话框中。每当我按下save,应用程序就会崩溃
我知道这与我的onClick方法有关,在onClick方法中,我将所有输入设置为字符串,以便将它们写入对话框。当我尝试将数据从微调器、单选按钮和切换按钮转换为字符串时,似乎发生了这种情况。当我注释掉这些内容时,对话框可以很好地处理编辑文本数据
有人知道怎么了吗
Java代码
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.ToggleButton;
import 安卓.widget.Spinner;
import 安卓.widget.RadioButton;
import 安卓.widget.RadioGroup;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.AdapterView;
import 安卓.widget.AdapterView.OnItemSelectedListener;
import 安卓.content.DialogInterface;
import 安卓.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, 安卓.R.layout.simple_spinner_item, categories);
adapter.setDropDownViewResource(安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin" >
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Title rows - Text View
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<TextView
安卓:text="Software News"
安卓:textSize="22sp"
安卓:textStyle="bold"
安卓:gravity="center"
安卓:layout_gravity="center"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:padding="0dp"
安卓:layout_margin="4dp"/>
<TextView
安卓:text="Mobile App Reviewer"
安卓:textSize="20sp"
安卓:textStyle="bold"
安卓:gravity="center"
安卓:layout_gravity="center"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:padding="0dp"
安卓:layout_margin="4dp"/>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App info
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App name - Edit text
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
安卓:gravity="left"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal" >
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Application: "
安卓:textStyle="bold"
安卓:textSize="14sp"
安卓:gravity="left"
安卓:layout_gravity="top"
安卓:padding="4dp"
安卓:layout_margin="4dp" />
<EditText
安卓:id="@+id/etAppName"
安卓:textSize="16sp"
安卓:background="#33CCCC"
安卓:gravity="left"
安卓:layout_gravity="left"
安卓:layout_width="100dp"
安卓:layout_height="25dp"
安卓:padding="0dp"
安卓:layout_margin="4dp" />
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App Category - Spinner
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
安卓:gravity="left"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:layout_margin="6dp" >
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Category: "
安卓:textStyle="bold"
安卓:textSize="14sp"
安卓:gravity="left"
安卓:layout_gravity="top"
安卓:padding="4dp"
安卓:layout_margin="4dp" />
<Spinner
安卓:id="@+id/spCategory"
安卓:layout_width="160dp"
安卓:layout_height="wrap_content" />
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
App Rating - Radio Buttons
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
安卓:gravity="left"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:layout_margin="6dp" >
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Rating: "
安卓:textStyle="bold"
安卓:textSize="14sp"
安卓:gravity="left"
安卓:layout_gravity="top"
安卓:padding="4dp"
安卓:layout_margin="4dp" />
<RadioGroup
安卓:id="@+id/rgRating"
安卓:gravity="left"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical" >
<RadioButton
安卓:id="@+id/rbGood"
安卓:text="Good"
安卓:textSize="14sp"
安卓:textStyle="bold"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:onClick="onRadioButtonClicked" />
<RadioButton
安卓:id="@+id/rbFair"
安卓:text="Fair"
安卓:textSize="14sp"
安卓:textStyle="bold"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:onClick="onRadioButtonClicked" />
<RadioButton
安卓:id="@+id/rbBad"
安卓:text="Bad"
安卓:textSize="14sp"
安卓:textStyle="bold"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:onClick="onRadioButtonClicked" />
</RadioGroup>
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Google Play? - Toggle Button
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
安卓:gravity="left"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:layout_margin="6dp" >
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="On Google Play? "
安卓:textStyle="bold"
安卓:textSize="14sp"
安卓:gravity="left"
安卓:layout_gravity="top"
安卓:padding="4dp"
安卓:layout_margin="4dp" />
<ToggleButton
安卓:id="@+id/tbGooglePlay"
安卓:textOn="NO"
安卓:textOff="YES"
安卓:textSize="12sp"
安卓:textStyle="bold"
安卓:layout_width="75dp"
安卓:layout_height="wrap_content"/>
</LinearLayout>
<!--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Google Play? - Toggle Button
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<LinearLayout
安卓:gravity="left"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:layout_margin="6dp" >
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Price: $ "
安卓:textStyle="bold"
安卓:textSize="14sp"
安卓:gravity="left"
安卓:layout_gravity="top"
安卓:padding="4dp"
安卓:layout_margin="4dp" />
<EditText
安卓:id="@+id/etPrice"
安卓:textSize="16sp"
安卓:background="#33CCCC"
安卓:inputType="numberDecimal"
安卓:gravity="left"
安卓:layout_gravity="left"
安卓:layout_width="100dp"
安卓:layout_height="25dp"
安卓:padding="0dp"
安卓:layout_margin="4dp" />
</LinearLayout>
<Button
安卓:id="@+id/btnSave"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_margin="6dp"
安卓:background="#A0CFEC"
安卓:gravity="center"
安卓:padding="4dp"
安卓:text="Save"
安卓:textSize="14sp"
安卓:textStyle="bold" />
</LinearLayout>
# 1 楼答案
rgRating未分配给任何对象。因此,当调用getCheckedRadioButtonId时,您将得到一个nullPointerException
如果您检查堆栈跟踪或调试应用程序,这一点是显而易见的
要修复此问题,请在XML文件中为rgRating分配一个ID,并执行以下操作:
。。。在尝试调用getCheckedRadioButtonId()之前
# 2 楼答案
所以在@DKIT指出我在rgRating上的错误后,我注意到我在tbGooglePlay上也做了同样的事情
最后,将这些代码行添加到Java文件解决了我的问题:
# 3 楼答案
试试这个:
# 4 楼答案
改变这个
到