我正在创建一个模拟滚动不同类型骰子的应用程序。所有的骰子都是通过一个充满按钮的 AlertDialog 来选择的。我想为其中六个按钮使用一个 OnClickListener,但是当我尝试设置该侦听器时,eclipse 会抛出 NullPointerException。
package edu.elon.cs.diceroller;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class RollMenuActivity extends Activity {
private int diceAmount;
private int diceSides;
private boolean sum;
private Button addDiceButton;
private Button rollDiceButton;
private ListView diceListView;
private Button oneButton;
private Button twoButton;
private Button threeButton;
private Button fourButton;
private Button fiveButton;
private Button sixButton;
private Button d4;
private Button d6;
private Button d8;
private Button d12;
private Button d20;
private Button sumButton;
private Button top3Button;
private Button trimButton;
private Button avgButton;
private Button hiLoButton;
private Button loHiButton;
private TextView displayQuantity;
private TextView displayType;
private TextView displayArrange;
private OnClickListener addDiceListener = new OnClickListener() {
@Override
public void onClick(View aArg0) {
AlertDialog.Builder alert = new AlertDialog.Builder(
RollMenuActivity.this);
LayoutInflater factory = LayoutInflater
.from(RollMenuActivity.this);
View layout = factory.inflate(R.layout.dice_select, null);
alert.setView(layout);
alert.show();
}
};
private OnClickListener rollDiceListener = new OnClickListener() {
@Override
public void onClick(View aV) {
// TODO Auto-generated method stub
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_roll_menu, menu);
return true;
}
private OnClickListener quantityListener = new OnClickListener(){
@Override
public void onClick(View v) {
Button aButton = (Button) v;
String buttonNumber = aButton.getText().toString();
diceAmount = Integer.parseInt(buttonNumber.trim());
displayQuantity.setText(buttonNumber);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roll_menu);
addDiceButton = (Button) findViewById(R.id.adddicebutton);
rollDiceButton = (Button) findViewById(R.id.rolldicebutton);
diceListView = (ListView) findViewById(R.id.dicelistview);
addDiceButton.setOnClickListener(addDiceListener);
rollDiceButton.setOnClickListener(rollDiceListener);
oneButton = (Button) findViewById(R.id.button1);
twoButton = (Button) findViewById(R.id.button2);
threeButton = (Button) findViewById(R.id.button3);
fourButton = (Button) findViewById(R.id.button4);
fiveButton = (Button) findViewById(R.id.button5);
sixButton = (Button) findViewById(R.id.button6);
oneButton.setOnClickListener(quantityListener);
twoButton.setOnClickListener(quantityListener);
threeButton.setOnClickListener(quantityListener);
fourButton.setOnClickListener(quantityListener);
fiveButton.setOnClickListener(quantityListener);
sixButton.setOnClickListener(quantityListener);
displayQuantity = (TextView) findViewById(R.id.displayquantity);
}
}
<RelativeLayout 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"
tools:context=".RollMenuActivity" >
<ListView
android:id="@+id/dicelistview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/adddicebutton" >
</ListView>
<Button
android:id="@+id/rolldicebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Press or Shake to Roll Dice"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/adddicebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/rolldicebutton"
android:layout_alignParentLeft="true"
android:text="Add a Dice"
tools:ignore="HardcodedText" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/diceselectlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="@+id/buttond4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="d4" />
<Button
android:id="@+id/buttonsum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Sum " />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout5"
android:layout_centerHorizontal="true" >
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:id="@+id/buttond20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="d20" />
<Button
android:id="@+id/buttonhilo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi-Lo" />
</LinearLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Type"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout2"
android:layout_alignParentTop="true"
android:text="Quantity"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/linearLayout2"
android:text="Arrange"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout2"
android:layout_below="@+id/linearLayout2" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="@+id/buttond6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="d6" />
<Button
android:id="@+id/buttontop3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top 3" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout3"
android:layout_below="@+id/linearLayout3" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:id="@+id/buttond8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="d8" />
<Button
android:id="@+id/buttontrim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trim " />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout4"
android:layout_below="@+id/linearLayout4" >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="@+id/buttond12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="d12" />
<Button
android:id="@+id/buttonavg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Avg. " />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true" >
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:id="@+id/button17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
<Button
android:id="@+id/buttonlohi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lo-Hi" />
</LinearLayout>
<TextView
android:id="@+id/displayquantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout5"
android:layout_below="@+id/linearLayout6"
android:layout_marginTop="17dp"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/displaytype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_centerHorizontal="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/displayarrange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignRight="@+id/linearLayout2"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>