我有两个按钮显示在我的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();
}
}
答案 0 :(得分:2)
Spinner tasteSpinner = (Spinner)findViewById(R.id.spinn
Spinner ammountSpinner = (Spinner)findViewById(R.id.spinner2);
如果对话框中的上述微调器意味着将其更改为,
Spinner tasteSpinner = (Spinner) dialog.findViewById(R.id.spinn
Spinner ammountSpinner = (Spinner) dialog.findViewById(R.id.spinner2);
如果你在下面添加微调器初始化spinner2要好得多,而不是在按钮点击内。
答案 1 :(得分:0)
你的错误应该在以下几行:
Spinner tasteSpinner = (Spinner)findViewById(R.id.spinner1);
Spinner ammountSpinner = (Spinner)findViewById(R.id.spinner2);
您正在使用对话框微调器,因此您必须使用如下对话框进行初始化:
Spinner tasteSpinner = (Spinner)dialog.findViewById(R.id.spinner1);
Spinner ammountSpinner = (Spinner)dialog.findViewById(R.id.spinner2);
答案 2 :(得分:0)
请改变这件事。
String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);
以强>
String userName = prefs.getString("userName", "");
String userID = prefs.getString("userID", "");
如果您在对话框中旋转,则更改。
Spinner tasteSpinner = (Spinner) dialog.findViewById(R.id.spinn
Spinner ammountSpinner = (Spinner) dialog.findViewById(R.id.spinner2);
答案 3 :(得分:0)
AlertDialog
使用构建器模式,使用起来更容易。AlertDialog
在按下按钮时解散,无需明确调用。始终在非空常量上调用equals()
,并将另一个对象作为参数传递。
private void AddTaste(){
final View v = getLayoutInflater().inflate(R.layout.add_taste_dialog,null);
final Spinner s1 = (Spinner) v.findViewById(R.id.spinner1);
final Spinner s2 = (Spinner) v.findViewById(R.id.spinner2);
final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String taste = String.valueOf(s1.getSelectedItem());
String amount = String.valueOf(s2.getSelectedItem());
AcceptTaste(taste,amount);
}
};
new AlertDialog.Builder(this)
.setTitle("Add Taste")
.setView(v)
.setNegativeButton("Cancel",null)
.setPositiveButton("Add Taste",listener)
.show();
}
private void AcceptTaste(String taste, String amount){
int a = 0;
if("A Hint".equals(amount)){
a= 1;
}
//--- more stuff---
}