2013-05-06 24 views
7

我經歷對話框上谷歌的Android開發者頁面的價值,特別是this部分。然而,而不是編程方式創建的DialogFragment的消息時,我做了一個名爲layout_newpayperiod.xml具有以下元素預先設置的佈局:從檢索中的EditText DialogFragment

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<Spinner 
    android:id="@+id/spinner_payperiod" 
    android:layout_width="fill_parent" 
    android:layout_height="48dp" 
    android:padding="8dp" 
    android:entries="@array/pay_periods" 
    /> 

<EditText 
    android:id="@+id/edittext_savepercent" 
    android:layout_width="fill_parent" 
    android:layout_height="48dp" 
    android:padding="8dp" 
    android:inputType="number" 
    android:hint="Percent to Save" 
    /> 

<EditText 
    android:id="@+id/edittext_payment" 
    android:layout_width="fill_parent" 
    android:layout_height="48dp" 
    android:padding="8dp" 
    android:inputType="numberDecimal" 
    android:hint="Total Payment" 
    /> 

</LinearLayout> 

當我打電話DialogFragment它顯示爲正常,具有正確的價值觀的微調。我填寫了條目並點擊「確定」,但是當我嘗試從微調器和兩個EditText字段中檢索值時,應用程序部隊以NumberFormatException: Invalid double ""關閉。我感覺我沒有正確地檢索視圖。任何人都可以幫我解決這個問題嗎?謝謝!

public class StartPayperiodDialogFragment extends DialogFragment { 

/* The activity that creates an instance of this dialog fragment must 
* implement this interface in order to receive event callbacks. 
* Each method passees the DialogFragment in case the host needs to query it. 
*/ 
public interface StartPayperiodDialogListener{ 
    public void onDialogPositiveClick(DialogFragment dialog); 
    public void onDialogNegativeClick(DialogFragment dialog); 
} 

// Use this instance of the interface to deliver action events 
StartPayperiodDialogListener listener; 

// Override the Fragment.onAttach() method to instantiate the StartPayperiodDialogListener 
@Override 
public void onAttach(Activity activity){ 
    super.onAttach(activity); 
    // Verify that the host activity implements the callback interface 
    try{ 
     // Instantiate the NoticeDialogListener so we can send events to the host 
     listener = (StartPayperiodDialogListener) activity; 
    }catch(ClassCastException e){ 
     // The activity doesn't implement the interface, throw exception 
     throw new ClassCastException(activity.toString() + " must implement StartPayperiodDialogListener"); 
    } 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState){ 
    // Use the Builder class for convenient dialog construction 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    View transactionLayout = View.inflate(getActivity(), R.layout.layout_newpayperiod, null); 
    builder.setView(transactionLayout) 
    .setPositiveButton("OK", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // Send the positive button event back to the calling activity 
      listener.onDialogPositiveClick(StartPayperiodDialogFragment.this); 
     } 
    }) 
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // Send the negative button event back to the calling activity 
      listener.onDialogNegativeClick(StartPayperiodDialogFragment.this); 
     } 
    }); 
    return builder.create(); 
} 

}

在MainActivity.class,回調方法:

@Override 
public void onDialogPositiveClick(DialogFragment dialog) { 
    // User pressed OK, so we need to grab the values from the 
    // dialog's fields and apply them to the Views in the Main 
    // Activity 

    View transactionLayout = View.inflate(this, R.layout.layout_newpayperiod, null); 

    // Start with the payment amount 
    EditText paymentEt = (EditText) transactionLayout.findViewById(R.id.edittext_payment); 
    TextView paymentTv = (TextView) findViewById(R.id.text_paycheck); 
    paymentTv.setText(moneyFormat.format(Double.parseDouble(paymentEt.getText().toString()))); 

    // Next, the percent to save 
    EditText savingsEt = (EditText) transactionLayout.findViewById(R.id.edittext_savepercent); 
    TextView savingsTv = (TextView) findViewById(R.id.text_savings); 
    savingsTv.setText(savingsEt.getText().toString() + "%"); 

    // Then, the pay period 
    Spinner periodSp = (Spinner) transactionLayout.findViewById(R.id.spinner_payperiod); 
    TextView periodTv = (TextView) findViewById(R.id.text_payperiod); 
    periodTv.setText(periodSp.getSelectedItem().toString()); 

    // Finally, let's update the daily allowance amount and clear 
    // the adapter 
    adapter.clear(); 
    adapter.notifyDataSetChanged(); 
    TextView allowanceTv = (TextView) findViewById(R.id.text_allowance); 
    Double allowanceValue; 
    switch(periodSp.getSelectedItemPosition()){ 
     case(0): // Daily 
      allowanceValue = Double.parseDouble(paymentTv.getText().toString()); 
      break; 
     case(1): // Weekly 
      allowanceValue = Double.parseDouble(paymentTv.getText().toString())/7; 
      break; 
     case(2): // 2 Weeks 
      allowanceValue = Double.parseDouble(paymentTv.getText().toString())/14; 
      break; 
     case(3): // 30 Days 
      allowanceValue = Double.parseDouble(paymentTv.getText().toString())/30; 
      break; 
     default: // Debugging purposes only 
      allowanceValue = 42.0; 
      break; 
    } 
    allowanceTv.setText(Double.toString(allowanceValue)); 
} 

回答

18

試試這個:

@Override 
public void onDialogPositiveClick(DialogFragment dialog) { 
    // User pressed OK, so we need to grab the values from the 
    // dialog's fields and apply them to the Views in the Main 
    // Activity 

    // Start with the payment amount 
    Dialog dialogView = dialog.getDialog(); 
    EditText paymentEt = (EditText) dialogView.findViewById(R.id.edittext_payment); 

...等(通過同樣的方式查詢dialogView檢索對話框任何其他意見。)

你的充氣代碼「膨脹」的新品牌該視圖的版本。你想訪問在對話框中創建的那個。

+0

我給它一個鏡頭,但我得到的'的EditText paymentEt =(EditText上)dialogView.findViewById(R.id.edittext_payment)一個NullPointerException周杰倫博布欽的答案;'線。我懷疑dialog.getView()調用沒有做我們所期望的... – Argus9 2013-05-07 01:19:16

+1

@ Argus9你是對的。這將是正常片段的正確用法,但對於DialogFragment,則需要使用getDialog()。我編輯了我的答案來反思。 – 2013-05-07 15:30:02

+0

絕對的天才迴應,謝謝Jay! – Argus9 2013-05-07 16:18:40

1

我認爲,這條線View transactionLayout = View.inflate(this, R.layout.layout_newpayperiod, null);食堂的一切。也許這不是搞亂,但你得到新創建的佈局地址,並將其分配給transactionLayout參考。然後,您將從佈局EditText paymentEt = (EditText) transactionLayout.findViewById(R.id.edittext_payment);獲取視圖,這些視圖肯定是未初始化的。它具有值空字符串值 - >「」;

我認爲你應該使用findViewById獲得關於你的EditText的爲你與你的TextView的事情。但是,因爲您的MainActivity版本可能不是您的R.layout.layout_newpayperiod的父視圖,所以您必須找到正確的方法。

您已在此onDialogPositiveClick回調方法中獲得DialogFragment作爲參數。所以,你可以得到它的View,你要尋找的佈局 - 包含您的EditText的

對不起,編輯這個職位這麼多次。

+0

我想我沒有選擇這樣做的唯一原因是因爲使用findViewById當像我一樣用TextViews,我得到一個NullPointerException。就好像元素存在於R.id中,但應用程序在運行時無法找到它們。我會再試一次,看看它是否有幫助。 – Argus9 2013-05-06 21:58:22

+0

哦,同樣 - TextViews出現在MainActivity的佈局XML文件中,該文件在Activity的onCreate()方法中的SetContentView()中調用。 – Argus9 2013-05-06 22:01:20

+0

你得到空引用,因爲這些視圖彼此不相關。看看應該解決您的問題 – Garet 2013-05-06 22:06:41

相關問題