2016-06-09 33 views
0

我有一個對話框片段,它通過使用接口將一個double值傳遞給另一個片段,但在我的另一個片段中,它獲得無效的double值。如何從片段發送一個雙值到其他

接口:

public interface Receive { 

    public void onDataReceive(Double out); 
} 

我的片段1:

public class InputFragment extends DialogFragment { 

    private Receive mData; 
    EditText ed; 
    RadioButton rb1,rb2; 
    Button b1,b2; 


    public InputFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     mData = (Receive)activity; 
    } 


    @NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     return super.onCreateDialog(savedInstanceState); 
    } 

    private void sendDataToFrag(){ 
     b2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (!ed.getText().toString().equals("")) { 
        if (rb1.isChecked()) { 
         int input = Integer.valueOf(ed.getText().toString()); 
         double out = input/24.0; 
         out = Double.parseDouble(new DecimalFormat("#.0").format(out)); 
         mData.onDataReceive(out); 
        } else if (rb2.isChecked()) { 
         int input = Integer.valueOf(ed.getText().toString()); 
         double out = ((input * 2/3) * 0.029574); 
         out = Double.parseDouble(new DecimalFormat("#.0").format(out)); 
         mData.onDataReceive(out); 
        } 
       } 
      } 
     }); 
     b1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dismiss(); 
      } 
     }); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.input, container, false); 
     ed = (EditText)view.findViewById(R.id.editText); 
     rb1 = (RadioButton)view.findViewById(R.id.radioButton1); 
     rb2 = (RadioButton)view.findViewById(R.id.radioButton2); 
     b1 = (Button)view.findViewById(R.id.cancel); 
     b2 = (Button)view.findViewById(R.id.confirm); 

     sendDataToFrag(); 
     return view; 
    } 

在我的主要活動調用接口:

@Override 
public void onDataReceive(Double out) { 
      MainScreen mainScreen = (MainScreen) getSupportFragmentManager().findFragmentById(R.id.ms); 
      mainScreen.getData(out); 
} 

在我的另一fragment2:

公共無效getDa ta(double out){ result = Double.toString(out);我拿雙串

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.main_screen, container, false); 
    mainScreen = this; 
    textView = (TextView)view.findViewById(R.id.res); 
    textView.setText(result); 
    ram = Double.parseDouble(textView.getText().toString()); // here I'm retrieving string to a double but getting exception invalid double.. 

例外:

FATAL EXCEPTION: main Process: com.cloudicalabs.waterfinder.water_rem, PID: 2883 
    java.lang.NumberFormatException: Invalid double: "" 
    at java.lang.StringToReal.invalidReal(StringToReal.java:63) 
    at java.lang.StringToReal.parseDouble(StringToReal.java:267) 
    at java.lang.Double.parseDouble(Double.java:301) 
    at com.cloudicalabs.waterfinder.water_rem.MainScreen.onCreateView(MainScreen.java:42) 
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 

回答

0

你忘了從textView

1)檢查輸入不要忘記檢查null爲防止NullPointerException異常。

if (null != textView.getText()) { 
    //... 
} 

2)不要忘記檢查empty字符串以防止NumberFormatException。

if (!textView.getText().toString().isEmpty()) { 
    //... 
} 

最後,解析雙碼:

public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    //... 
    ram = getDouble(textView); 
} 

private double getDouble(TextView textView) { 
    double result = 0.0; 
    if (null != textView.getText() && !textView.getText().toString().isEmpty()) { 
     try { 
      result = Double.parseDouble(textView.getText().toString()); 
     } catch (NumberFormatException e) { 
      e.printStackTrace(); 
     } 
    } 
    return result; 
} 

一哈,我找到的東西是錯在這裏。 你的片段之間的通信不順利。 目前,我已經看到:

1)InputFragment將獲得輸入並調用Activity的回調。

2)活動結果發送到片​​段MainScreen

3)你想顯示從MainScreen的onCreateView數據。

問題發生在第3步。 解決它。我們有兩個選擇:

1)如果MainScreen的用戶類型輸入前附:你應該在方法getData

public void getData(double out) { 
    ram = out; 
    // do something with ram (ex: set to textview) 
} 

2)更新數據,如果MainScreen沒有用戶類型輸入之前連接,你應該數據直接發送到MainScreen您連接到您的活動

+0

我試過了,但在我的@Override中顯示空對象引用 public void onDataReceive(Double out){MainScreen mainScreen =(MainScreen)getSupportFragmentManager()。findFragmentById(R.id.ms); mainScreen.getData(out);並在我的片段1:mData.onDataReceive(out); – Siva

0
try { 
    ram= Double.valueOf(textView.getText().toString()); 
} catch (NumberFormatException e) { 
    inp = 0; 
} 
+0

我試過,但顯示我的@覆蓋 公共無效onDataReceive(雙出){ MainScreen mainScreen =(MainScreen)getSupportFragmentManager()。findFragmentById(R空對象引用前.id.ms); mainScreen.getData(out);並在我的片段1:mData.onDataReceive(out); – Siva

0

試試這個,

刪除代碼ram = Double.parseDouble(textView.getText().toString());

並與下面的代碼替換,

String text = textView.getText().toString(); 
ram = getDoubleValue(text); 

添加下面的方法在您的類

private double getDoubleValue(String text) { 
     Double value = 0.0; 
     try { 
      value = Double.parseDouble(text); 
     } catch (NumberFormatException e) { 
      e.printStackTrace(); 
     } 
     return value; 
    } 
0

也許onCreateView方法執行時,該getData還沒有被調用。因此result的值在執行onCreateView方法時始終爲""

試試這個:

public void getData(double out) { 
    ram = out; 
    textView.setText(String.valueOf(out)); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.main_screen, container, false); 
    mainScreen = this; 
    textView = (TextView)view.findViewById(R.id.res); 
    // ... 
} 
相關問題