应用程序崩溃选择微调项目?

时间:2014-03-04 06:06:24

标签: android jquery spinner alertdialog

在我的应用程序中,我为Templates提供了各种Text Message Bodyspinner list items,可以选择,用户可以发送它们而不是键入message,但问题是当用户打开菜单项以选择application crashes模板时。 Spinner我已放入alert dialog box,可通过菜单项访问。

对话框代码*

AlertDialog.Builder rdialog = new AlertDialog.Builder(MainActivity.this);
            rdialog.setTitle("Select Message");
            rdialog.setIcon(android.R.drawable.ic_input_get);
            LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
            alertView = inflater.inflate(R.layout.rptsetting,null);

            final Spinner fSpinner = (Spinner)alertView.findViewById(R.id.fSpinner);
            String providers[] ={"Busy", "Good Morning", "In office"};
            ArrayAdapter<String> adp = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_dropdown_item,providers);
            fSpinner.setAdapter(adp);
            fSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> aparent, View arg1,
                        int pos, long arg3) {

                    String selectedItem = fSpinner.getSelectedItem().toString();
                    if(selectedItem.equals("Busy")){
                        body = "Currently Busy call again later, Thanks";
                    }

                    if(selectedItem.equals("Good Morning")){
                        body = "A very Good Morning, Have a nice day";
                    }

                    if(selectedItem.equals("In office")){
                        body = "Currently in office";
                    }   

                }

                @Override
                public void onNothingSelected(AdapterView<?> aparent) {
                }
            });
rdialog.setView(alertView);
            rdialog.setNeutralButton("SUBMIT", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();   
                    }
                });

            AlertDialog rdialog1 = rdialog.create();
            rdialog1.show();

我将body定义为全局字符串,以便Sms Manager可以访问它以将其用作要发送的消息正文。 记录猫 Log cat

提前致谢!

2 个答案:

答案 0 :(得分:2)

首先在onItemSelected(.....)

下修正此项
  String selectedItem = aparent.getItemAtPosition(pos).toString();

  if(selectedItem.equals("Busy")){
  body = "Currently Busy call again later, Thanks";
   }
  if(selectedItem.equals("Good Morning")){
  body = "A very Good Morning, Have a nice day";
   }
  if(selectedItem.equals("In office")){
  body = "Currently in office";
  }   

并交叉检查您的body变量是否为空

答案 1 :(得分:1)

要从Spinner获取所选项目,请尝试使用getItemAtPosition AdapterView方法。为:

@Override
  public void onItemSelected(AdapterView<?> aparent, View arg1,
                        int pos, long arg3) {
 String selectedItem = aparent.getItemAtPosition(pos).toString();
 //...your code...
}