2014-03-04 93 views
1

在我的申請,我爲Text Message Body提供各種Templates,作爲spinner list items,可以選擇和用戶可以把他們而不是鍵入message,但問題是,當用戶打開菜單項選擇模板application crashesSpinner我已通過菜單項訪問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作爲全球String,它可以訪問由Sms Manager使用它作爲郵件的正文被髮送。 Log Cat Log cat

在此先感謝!

+1

發表您的logcat –

+0

@ Simple Plan的檢查我的編輯日誌貓 –

+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變量不爲空

+0

我已經聲明'body'爲'String body;' –

+0

@Androidbeginner先試試這個,並給我反饋 –

+0

已經嘗試過,即使我想連接編輯文本值與給定的字符串像一些文本與給定的模板代碼 - 「」目前在辦公室「+ edittext.getText()。toString()'它顯示錯誤。 –

1

要從Spinner中選擇物品,請嘗試使用getItemAtPosition方法AdapterView。如:

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