0

我有两个按钮显示在我的 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();


    }







}
4

4 回答 4

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 下面添加微调器初始化会好得多,而不是在按钮单击内部。

于 2013-06-29T02:44:50.753 回答
0
  1. AlertDialog使用构建器模式并且使用起来更容易。
  2. AlertDialog按下按钮时关闭,无需显式调用它。
  3. 始终调用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---
    }
    
于 2013-06-29T05:28:42.070 回答
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);
于 2013-06-29T04:28:39.823 回答
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);
于 2013-06-29T05:01:58.900 回答