2016-06-09 2 views
0

인터페이스를 사용하여 다른 단편에 이중 값을 전달하는 대화 상자 단편은 있지만 다른 단편에서는 유효하지 않은 이중 값을 가져 오는 대화 상자 단편이 있습니다.단편에서 다른 단으로 double 값을 보내는 방법

인터페이스 :

public interface Receive { 

    public void onDataReceive(Double out); 
} 

내 fragment1 : 내 주요 활동 호출 인터페이스에서

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

: 내 다른 fragment2에서

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

:

공공 무효 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

당신은 방지 NullPointerException이에 대한 null을 확인하는 것을 잊지 마십시오 textView

1)에서 입력을 확인 잊어 버렸습니다.

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

2) NumberFormatException이를 방지하기 위해 empty 문자열을 확인하는 것을 잊지 마십시오.

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의 onCreateView에서 데이터를 표시 할 조각 MainScreen

3)에 결과를 보냅니다.

3 단계로 인해 문제가 발생했습니다. 해결하려면. MainScreen이 사용자 유형 입력하기 전에 부착되지 않은 경우 당신이해야, 당신은 방법 getData

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

2)에 데이터를 업데이트하십시오 MainScreen이 사용자 유형 입력하기 전에 연결되어있는 경우

1) : 우리는 두 가지 옵션이 있습니다 당신은 내가 시도했지만 내 @Override 공공 무효 onDataReceive (교체 아웃 DOUBLE) { MainScreen mainScreen = (MainScreen)에서 getSupportFragmentManager(). findFragmentById (R에 null 객체 참조를 보여주는

+0

내 오버 라이드에 null 객체 참조를 표시했습니다. public void onDataReceive (Double out) { MainScreen mainScreen = (MainScreen) getSupportFragmentManager(). findFragmentById (R.id.ms); mainScreen.getData (out); 그리고 내 조각 1 : mData.onDataReceive (개); – Siva

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

활동에 부착하기 전에 MainScreen에 직접 데이터를 전송 .id.ms); mainScreen.getData (out); 그리고 내 조각 1 : mData.onDataReceive (개); – Siva

0

이 시도

제거 코드 ram = Double.parseDouble(textView.getText().toString());

그리고 코드 아래로 교체

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

는 클래스 onCreateView 방법이 실행될 때 아마의 getData가 호출되지 않은

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

에 방법을 아래에 추가합니다. 따라서 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); 
    // ... 
} 
관련 문제