对话框中按钮上的onclick侦听器强制关闭应用程序 [英] onclick listener on button in dialog force closes app

查看:78
本文介绍了对话框中按钮上的onclick侦听器强制关闭应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个按钮,它们显示在我的android应用程序中弹出的对话框中.我的问题是我的一个按钮上的onclick侦听器起作用,而另一按钮关闭了.

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.

我得到的错误是:

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类是:

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


    }







}

推荐答案

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

如果在spinner2下方添加Spinner初始化要好得多,而不是在按钮内部单击.

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

这篇关于对话框中按钮上的onclick侦听器强制关闭应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆