onclick listener on button in dialog force closes app

依然范特西╮ 提交于 2019-12-11 19:35:02

问题


I have two buttons that show in a dialog pop up in my android app. My problem is one of my onclick listeners for one button works, while the other force closes.

The error I get is:

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)

My java class that launches the dialog and handles the click is:

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();


    }







}

回答1:


Spinner tasteSpinner = (Spinner)findViewById(R.id.spinn
Spinner ammountSpinner = (Spinner)findViewById(R.id.spinner2);

If the above spinners inside the dialog means change it as,

Spinner tasteSpinner = (Spinner) dialog.findViewById(R.id.spinn
Spinner ammountSpinner = (Spinner) dialog.findViewById(R.id.spinner2);

If you add spinner initialization at below spinner2 is much better instead of inside the button click.




回答2:


your error should be on below lines:

Spinner tasteSpinner = (Spinner)findViewById(R.id.spinner1);
Spinner ammountSpinner = (Spinner)findViewById(R.id.spinner2);

you are using dialog spinners so you must have to initialise with dialog like below:

Spinner tasteSpinner = (Spinner)dialog.findViewById(R.id.spinner1);
Spinner ammountSpinner = (Spinner)dialog.findViewById(R.id.spinner2);



回答3:


Please change this thing.

String userName = prefs.getString("userName", null);
String userID = prefs.getString("userID", null);

to

 String userName = prefs.getString("userName", "");
 String userID = prefs.getString("userID", "");

If you spinner inside dialog then change.

Spinner tasteSpinner = (Spinner) dialog.findViewById(R.id.spinn
Spinner ammountSpinner = (Spinner) dialog.findViewById(R.id.spinner2);



回答4:


  1. AlertDialog uses builder pattern and has a much easier usage.
  2. AlertDialog dismisses on button press, need not call it explicitly.
  3. Always call equals() on a non null constant, and pass the other object as parameter.

    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---
    }
    


来源:https://stackoverflow.com/questions/17376147/onclick-listener-on-button-in-dialog-force-closes-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!