使用片段FuzFragment中的按钮设置if else语句

时间:2017-10-17 15:26:39

标签: android android-fragments

首先,我必须说我的英语不好,而且#34;全新的"到Android编程。

我想创建一个可以监控服务器性能的应用。我使用导航抽屉作为我的app界面。每个都有一些运行不同活动集的片段。其中一个片段,我想创建一个活动,可以使用if else语句计算计算服务器性能,并使用按钮提交结果。当我运行我的应用程序时,我遇到了这个片段(FuzFragment)的问题,其中我的应用程序立即停止并出现错误"不幸的是,ServerMonitorApp已停止"。

下面是我用来显示布局的片段类(FuzFragment):

package com.example.servermonitorapp;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    public class FuzFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }

        LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_fuz,
                container, false);

        Button sumButton = (Button) mLinearLayout.findViewById(R.id.submitButton);
        sumButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EditText cpu = (EditText) v.findViewById(R.id.textCPU);
                EditText ram = (EditText) v.findViewById(R.id.textRAM);
                TextView res = (TextView) v.findViewById(R.id.txtResult);

                int cpuslow = Integer.parseInt(cpu.getText().toString());
                int cpusmedium = Integer.parseInt(cpu.getText().toString());
                int cpushigh = Integer.parseInt(cpu.getText().toString());
                int ramlow = Integer.parseInt(ram.getText().toString());
                int rammedium = Integer.parseInt(ram.getText().toString());
                int ramhigh = Integer.parseInt(ram.getText().toString());

                if (cpuslow > 0 && cpuslow <= 30 | ramlow > 0 && ramlow <= 23) {
                    res.setText("Safe");

                } else if (cpusmedium > 30 && cpusmedium <= 60 | rammedium > 23 && rammedium <= 38) {
                    res.setText("Risk");

                } else if (cpushigh > 60 | ramhigh > 38) {
                    res.setText("Very Risk");

                } else {
                    res.setText("Invalid Number");
                }

            }
        });
        return mLinearLayout;
    }
    }

我的代码是否有任何错误导致我的应用停止响应?由于我还在学习Android编程,所以需要这方面的帮助。

2 个答案:

答案 0 :(得分:0)

欢迎来到Android黑暗的发展方面哈哈;)。 好的,我们来看几个基础知识。

onCreate方法用于充气或绘制布局。在绘制布局之前(如果你做了findViewById),它就不会存在。

为了使onCreate方法绘制图片,需要调用默认创建的setContent方法。如果不是您调用的第一行代码,这应该是其中之一。它确保在ACTIVITY的UI相关交互之后的所有内容都可用。强调活动,因为当你进入片段时规则会发生变化。

现在,下一个问题是你有臃肿的代码。做像这样的事情。

EditText myText = findViewById(R.id.myText);
int myValue = Integer.parseInt(myText.getText.toString());

等..都可以在同一行中完成,你没有在其他任何地方使用对myText的引用,所以就这样做:

int myValue = Integer.parseInt(findViewById(R.id.myText).getText.toString());

请记住我在做伪代码。请不要回答&#34;您的代码中有错误&#34;哈哈,否则我不会帮忙。

接下来,您似乎从未在onCreate中执行过setContentView方法,请将其放回原处,并将内容设置为与您正在膨胀的布局相匹配的activity.xml代码。

接下来,您在按钮单击中执行findViewById。这是不必要的重复代码。如果您需要反复使用textView,则存储对它的引用以避免重复查找。

class MyClass{

EditText myTextBox;

protected void onCreate(stuff){
     myTextBox = findViewById(R,id.myTextBox);

     findViewById(R.id.myButton).setOnClickListener(new OnClickListener()){
            @Override
            protected void onClick(){
                 int myValue = Integer.parseInt(myTextBox.getText().toString());
            }

     });
}

同样为了记录,onCreate应该非常干净而且非常重要。我通常有一个syncUI方法,可以在数据绑定日之前完成我的findViewById调用&#34;&#34;。我不再这样做,但新人学习很好。

然后在我的syncUI中,我调用包装器方法来处理单击侦听而不是在onCreate中嵌套,但是现在你正在学习。但如果你想要一个简单的例子..

onCreate(){
   setContentView(myViewPointer from the R File);
   syncUI();

}

private void syncUI(){
    //SETUP TEXTVIEWS OR OTHER UIs that you need
    //get btnSubmit reference from findViewByid

    btnSubmit_onClick(); //called one time in onCreate to wrap the click listener into method that allows it to collapse and be easily found.
}

private btnSubmit_onClick(){
    btnSubmit.setOnClickListener(new OnClickListener(){
         @Override
         protected void onClick(){
              //handle Clicks
         }

    });
}

答案 1 :(得分:0)

感谢Sam的回复..

我弄清楚我的一个问题导致我的片段在我运行我的应用程序时停止响应可能是因为我在上面的代码中错误地将我的布局声明为LinearLayout,而myfragment.xml文件中的实际布局在Relativelayout中(如下所示)。

&#13;
&#13;
LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_fuz,
                container, false);
&#13;
&#13;
&#13;

我纠正后,我的应用程序可以打开,上面相关的片段也可以打开。当我尝试使用少量数字样本运行代码并且我的应用程序停止响应并出现相同错误时,仅存在问题仍然存在问题&#34;不幸的是,ServerMonitorApp已停止&#34;。