在片段中设置带if按钮的if else语句FuzFragment
首先,我必须说我的英语不好,对Android编程“全新”。在片段中设置带if按钮的if else语句FuzFragment
我想创建一个可以监控服务器性能的应用程序。我使用导航抽屉作为我的应用程序界面。每个都有几个运行不同活动的片段。其中一个片段,我想创建一个活动,可以使用一些if语句计算并使用按钮提交结果来计算服务器性能。当我运行我的应用程序时,我遇到了这个片段(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编程,因此需要很多帮助。
欢迎来到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());
记住我做的伪代码。请不要让那个回复“你的代码有错误”的人哈哈,否则我不会帮忙。
接下来,看起来您从未在您的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调用“数据绑定日之前”调用。我不再那么做了,但新人学习没问题。
然后在我的syncUI中,我调用wrapper方法来处理点击监听,而不是在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
}
});
}
谢谢萨姆的回复..
我hv找出我的问题之一,导致我的片段停止响应时,我运行我的应用程序可能是由于我错误地声明我的布局为LinearLayout上面的代码在哪里我的myfragment.xml文件中的实际布局是Relativelayout(如下所示)。
LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_fuz,
container, false);
后,我纠正我的应用程序可以是开放的,能够上方也与该片段打开。当我尝试使用少量样本运行代码并且我的应用程序停止响应同一错误时,仍然存在问题“不幸的是,ServerMonitorApp已停止”。