我有两个按钮显示在我的 android 应用程序中弹出的对话框中。我的问题是我的一个按钮工作的 onclick 侦听器之一,而另一个力关闭。
我得到的错误是:
06-28 22:32:51.494: E/AndroidRuntime(29309): FATAL EXCEPTION: main
06-28 22:32:51.494: E/AndroidRuntime(29309): java.lang.NullPointerException
06-28 22:32:51.494: E/AndroidRuntime(29309): at com.example.beerportfoliopro.TasteTags$2.onClick(TasteTags.java:83)
06-28 22:32:51.494: E/AndroidRuntime(29309): at android.view.View.performClick(View.java:4102)
06-28 22:32:51.494: E/AndroidRuntime(29309): at android.view.View$PerformClick.run(View.java:17126)
06-28 22:32:51.494: E/AndroidRuntime(29309): at android.os.Handler.handleCallback(Handler.java:615)
06-28 22:32:51.494: E/AndroidRuntime(29309): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 22:32:51.494: E/AndroidRuntime(29309): at android.os.Looper.loop(Looper.java:155)
06-28 22:32:51.494: E/AndroidRuntime(29309): at android.app.ActivityThread.main(ActivityThread.java:5536)
06-28 22:32:51.494: E/AndroidRuntime(29309): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 22:32:51.494: E/AndroidRuntime(29309): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 22:32:51.494: E/AndroidRuntime(29309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
06-28 22:32:51.494: E/AndroidRuntime(29309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
06-28 22:32:51.494: E/AndroidRuntime(29309): at dalvik.system.NativeStart.main(Native Method)
我的启动对话框并处理点击的java类是:
public class TasteTags extends Activity {
BeerData e;
Dialog dialog = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tastetag_page);
//get beer data
Intent intent = getIntent();
Bundle b = intent.getExtras();
e = b.getParcelable("myBeerObject");
TextView beerTitle = (TextView) findViewById(R.id.beerTitleTaste);
beerTitle.setText(e.beerName + " Taste Profile");
String url = "myURL1";
url = url + "b=" +e.beerId;
//async task to get beer taste tag percents
new GetTasteJSON(this).execute(url);
}
public void addTaste(View v){
// custom dialog
dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);
dialog.setTitle("Add Taste");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
Button dialogButton2 = (Button) dialog.findViewById(R.id.addTasteButton);
// if button is clicked, close the custom dialog
dialogButton2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//get spinner text
Spinner tasteSpinner = (Spinner)findViewById(R.id.spinner1);
String tasteText = tasteSpinner.getSelectedItem().toString();
Spinner ammountSpinner = (Spinner)findViewById(R.id.spinner2);
String ammountText = ammountSpinner.getSelectedItem().toString();
int ammount = 0;
if(ammountText.equals("A Hint")){
ammount = 1;
}
if(ammountText.equals("Medium")){
ammount = 2;
}
if(ammountText.equals("Strong")){
ammount = 3;
}
//construct url
String url = "myURL";
String tasteURL = "&tasteNam=" + tasteText ;
String ammountURL = "&tasteNum=" + ammount;
String beerURL = "bID=" + e.beerId;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(TasteTags.this);
String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);
String userURL = "uID=" + userID;
url = url + userURL + beerURL + tasteURL + ammountURL;
//async task to submit tastes
new UpdateTaste(TasteTags.this, dialog).execute(url);
dialog.dismiss();
}
});
dialog.show();
}
}