2015-09-01 50 views
2

我在界面上显示输出时遇到了问题。无法显示所需的输出

我的程序应该能够在输入一些参数时显示相应的输出。如果没有输入参数,它将显示“没有结果”,这是我实施的。

但是,即使输入参数。它仍然显示“没有结果”,而不是根据公式给出的答案。

我的代码如下:

public class FreeSpaceActivity extends Fragment implements OnClickListener { 

    DecimalFormat disp_W = new DecimalFormat("0.000E0"); 
    DecimalFormat disp_dBm = new DecimalFormat("0.000"); 

    EditText tPowerEdit = null; 
    EditText tGainEdit = null; 
    EditText rGainEdit = null; 
    EditText freqEdit = null; 
    EditText distanceEdit = null; 
    EditText lightEdit = null; 
    Button calButton = null; 
    Button graphButton = null; 
    Button clearButton = null; 
    Button plotButton = null; 

    EditText radEdit = null;//testing 

    private ScrollView llLayout; 
    private FragmentActivity faActivity; 
    //private Switch switch1; 
    // Changes here 
    private double mResult = 0; 
    private Switch mSelectUnit; 
    private TextView mTextFreeSpaces; 
    private boolean mIsCalculated;// flag to show the result only when you click on calculate button 
    private boolean flag; 


    //@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     super.onCreate(null); 

     faActivity = (FragmentActivity) this.getActivity(); 
     llLayout = (ScrollView) inflater.inflate(R.layout.activity_free_space, container, false); 

     tPowerEdit = (EditText)llLayout.findViewById(R.id.editText1); 
     tGainEdit = (EditText)llLayout.findViewById(R.id.editText2); 
     rGainEdit = (EditText)llLayout.findViewById(R.id.editText3); 
     freqEdit = (EditText)llLayout.findViewById(R.id.editText4); 
     distanceEdit = (EditText)llLayout.findViewById(R.id.editText7); 
     lightEdit = (EditText)llLayout.findViewById(R.id.editText8); 
     calButton = (Button)llLayout.findViewById(R.id.button1); 
     calButton.setOnClickListener(this); 
     graphButton = (Button)llLayout.findViewById(R.id.button2); 
     graphButton.setOnClickListener(this); 
     clearButton = (Button)llLayout.findViewById(R.id.button3); 
     clearButton.setOnClickListener(this); 
     plotButton =(Button)llLayout.findViewById(R.id.plot_button); 
     plotButton.setOnClickListener(this); 

     radEdit = (EditText)llLayout.findViewById(R.id.editText28);//testing 

     // Changes here 
     mTextFreeSpaces = (TextView) llLayout.findViewById(R.id.textFreeSpaces); 

     mSelectUnit =(Switch)llLayout.findViewById(R.id.switch1); 
     mSelectUnit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       updateFreeSpacesText(isChecked); 
      } 
     }); 
     return llLayout; 
    } 

    /*private ActionBar getSupportActionBar() { 
     return null; 
    }*/ 

    private void updateFreeSpacesText(boolean isSwitcherChecked) { 
     if(mIsCalculated && flag==true){ 
      if(isSwitcherChecked){ 
       double dBm=10 * Math.log10(mResult* 1000); 
       mTextFreeSpaces.setText(disp_dBm.format(dBm));//display dbm 
       Toast.makeText(getActivity().getApplicationContext(),"Unit Chosen is dBm", Toast.LENGTH_LONG).show(); 
      } else { 
       double W=mResult; 
       mTextFreeSpaces.setText(disp_W.format(W));//display W 
       Toast.makeText(getActivity().getApplicationContext(),"Unit Chosen is W", Toast.LENGTH_LONG).show(); 

      } 
     } 

    } 

    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     super.onCreateOptionsMenu(menu, inflater); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     String tPowerStr = tPowerEdit.getText().toString(); 
     String tGainStr = tGainEdit.getText().toString(); 
     String rGainStr = rGainEdit.getText().toString(); 
     String freqStr = freqEdit.getText().toString(); 
     String distanceStr = distanceEdit.getText().toString(); 
     String lightStr = lightEdit.getText().toString(); 
     double tPower = Double.parseDouble(!tPowerStr.isEmpty() ? tPowerStr : "0"); 
     double tGain = Double.parseDouble(!tGainStr.isEmpty() ? tGainStr : "0"); 
     double rGain = Double.parseDouble(!rGainStr.isEmpty() ? rGainStr : "0"); 
     double freq = Double.parseDouble(!freqStr.isEmpty() ? freqStr : "0"); 
     double distance = Double.parseDouble(!distanceStr.isEmpty() ? distanceStr : "1"); 
     double light = Double.parseDouble(!lightStr.isEmpty() ? lightStr : "1"); 

     String radStr = radEdit.getText().toString();//testing 
     double radius = Double.parseDouble(!radStr.isEmpty() ? radStr : "0");//testing 


     if (v == calButton) { 
      if(mResult!=0) { 
       double lamda = 300000000/freq; 

       mResult = tPower * tGain * rGain * Math.pow(lamda/(4 * Math.PI * distance), 2)/light/100; 
       mIsCalculated = true; 
       flag = true; 
       updateFreeSpacesText(mSelectUnit.isChecked()); 
      } 
      else 
       mTextFreeSpaces.setText("No results"); 


     } 
     else if (v == graphButton) { 

      Intent intent = new Intent(getActivity(), GraphActivity.class); 
      intent.putExtra("model", "freeSpace"); 
      intent.putExtra("tp", tPower); 
      intent.putExtra("tg", tGain); 
      intent.putExtra("rg", rGain); 
      intent.putExtra("f", freq); 
      intent.putExtra("l", light); 
      this.startActivity(intent); 
     } else if (v == clearButton) { 
      flag=false; 
      double lamda = 300000000/freq; 

      ((EditText) llLayout.findViewById(R.id.editText1)).setText(null); 
      ((EditText) llLayout.findViewById(R.id.editText2)).setText(null); 
      ((EditText) llLayout.findViewById(R.id.editText3)).setText(null); 
      ((EditText) llLayout.findViewById(R.id.editText4)).setText(null); 
      ((EditText) llLayout.findViewById(R.id.editText7)).setText(null); 
      ((EditText) llLayout.findViewById(R.id.editText8)).setText(null); 
      ((EditText) llLayout.findViewById(R.id.editText28)).setText(null); 
      ((TextView) llLayout.findViewById(R.id.textFreeSpaces)).setText(null); 
      //((TextView) llLayout.findViewById(R.id.textFreeSpacePrd)).setText(""); 
      //mIsCalculated = true; 
      //updateFreeSpacesText(mSelectUnit.isChecked()); 
      //mResult=tPower * tGain * rGain * Math.pow(lamda/(4 * Math.PI * distance), 2)/light/100; 
      //mTextFreeSpaces.setText(""); 


     } else if (v == plotButton) { 
      //double radius = Double.parseDouble(radius.getText().toString()); 
      Intent intent = new Intent(getActivity(), MapActivity.class); 
      Bundle b = new Bundle(); 
      //b.putDouble("radius", result); 
      intent.putExtras(b); 
      this.startActivity(intent); 
     } 
    } 
} 

我的界面:

enter image description here

+0

实际上,如果mResults等于0,则设置为“无结果”。默认情况下,它将其初始化为0,并且我看不到您在任何地方更改其值。 – Stultuske

+0

@Stultuske,最初它会显示“NaN”,因为根据公式,所有变量已被初始化为0,这将导致数学错误。 – MkLim

+0

没有。 if(mResult!= 0){/ *你的代码在这里* /} else setText(“No result”); 和mResult等于0 – Stultuske

回答

0

我觉得Stultuske是正确的,但我的名誉是低置评。

,我宁愿你otherway你在else分支每次运行,因为你的变量mResult仍然是0,并没有改变改变IF-条款

 if (v == calButton) { 
     if(mResult!=0) { 
      . 
      . 
      . 
     } 
     else 
      mTextFreeSpaces.setText("No results"); 

的声明。 通过我感兴趣的是你的应用程序,因为我是一个双向无线电和卫星通信工程师的方式:

也许如果Edittexts不为空,像

  if(tPowerStr.matches("") && The other Edittexts) { 
       . 
       . 
       . 
      } 
      else 
       mTextFreeSpaces.setText("No results"); 

编辑您可以retreive。

+0

嗨,这是我的项目。我正在研究射频计算器,这是模块之一 – MkLim

+0

如果你是免费的,你可以花一些时间吗?我目前对射频开发并不熟悉,仅在我的课程中暴露于2个传播模块,即Free Space Loss&Two Ray。不过,我也很期待实施更多模型,如HATA。如果我能从你那里得到一些建议,会很高兴:-) – MkLim

+0

首先,你的程序现在可以正常工作了吗?问题解决了吗?之后,我们可以谈论RF。 :) – mcd