Android 在片段中设置带有按钮的if-else语句,FuzFragment
首先,我必须说我的英语不好,对Android编程“完全陌生” 我想创建一个可以监视服务器性能的应用程序。我已经使用导航抽屉作为我的应用程序界面。每一个都有几个片段与不同的活动集一起运行。其中一个片段,我想创建一个活动,它可以使用一些if-else语句计算来计算服务器性能,并通过按钮提交结果。当我运行我的应用程序时,我遇到了这个片段(FuzFragment)的问题,我的应用程序立即停止,出现了一个错误“不幸的是,ServerMonitorApp已停止” 下面是我用来显示布局的片段类(FuzFragment):Android 在片段中设置带有按钮的if-else语句,FuzFragment,android,android-fragments,Android,Android Fragments,首先,我必须说我的英语不好,对Android编程“完全陌生” 我想创建一个可以监视服务器性能的应用程序。我已经使用导航抽屉作为我的应用程序界面。每一个都有几个片段与不同的活动集一起运行。其中一个片段,我想创建一个活动,它可以使用一些if-else语句计算来计算服务器性能,并通过按钮提交结果。当我运行我的应用程序时,我遇到了这个片段(FuzFragment)的问题,我的应用程序立即停止,出现了一个错误“不幸的是,ServerMonitorApp已停止” 下面是我用来显示布局的片段类(FuzFrag
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;
}
}
package com.example.servermonitorapp;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类片段扩展了片段{
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
if(容器==null){
返回null;
}
线性布局mLinearLayout=(线性布局)充气器。充气(R.layout.FRAGENT_fuz,
货柜(虚假);;
按钮sumButton=(按钮)mLinearLayout.findViewById(R.id.submitButton);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
EditText cpu=(EditText)v.findViewById(R.id.textCPU);
EditText ram=(EditText)v.findviewbyd(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());
如果(cpuslow>0&&cpuslow 0&&ramlow 30&&cpusmedium 23&&rammedium 60 | ramhigh>38){
res.setText(“非常危险”);
}否则{
res.setText(“无效编号”);
}
}
});
返回布局;
}
}
我的代码是否有任何错误会导致我的应用程序停止响应?在这方面需要很多帮助,因为我还在学习Android编程。欢迎来到Android开发的黑暗面(哈哈;)。 好的,让我们来学习一些基本知识 onCreate方法用于充气或绘制布局。在绘制布局之前(如果使用findViewById),它将不存在 为了让onCreate方法绘制图片,它需要默认创建的名为setContent的方法。这应该是您调用的第一行代码之一。它确保在该行之后,活动的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中,我调用包装器方法来处理单击侦听,而不是嵌套在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
}
});
}
谢谢你的回复 我发现我运行应用程序时导致片段停止响应的一个问题可能是因为我在上面的代码中将布局声明为LinearLayout,否则myfragment.xml文件中的实际布局位于Relativelayout中(如下所示)
LinearLayout mLinearLayout=(LinearLayout)充气机。充气(R.layout.fragment_fuz,
货柜(虚假);代码>