我正在为物联网设备开发应用程序。我的应用程序具有不同的活动,用于从不同的传感器获取数据。 每个活动都通过单击按钮接收数据。每当我按下按钮时,都会出现以下错误。
confirm2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Get your custom_toast.xml layout
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));
//Initializing the text views
EditText editText = findViewById(R.id.link2);
final String link2 = editText.getText().toString();
EditText editText1 = findViewById(R.id.idvalue);
final String StringValue = editText1.getText().toString();
//Adding functionality to the text views
fire2 = new Firebase("https://iotsense.firebaseio.com/");
Firebase fireChild = fire2.child(link2+"/S1");
fireChild.setValue("4");
// Read from the database
myRef = FirebaseDatabase.getInstance().getReference();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
if(StringValue.length()>0 && link2.length()>0) {
try {
status = dataSnapshot.child(link2 + "/Analog").getValue().toString();}
catch (NullPointerException ignored){}
if(status==null){
TextView text4 = layout.findViewById(R.id.text4);
text4.setText(" Error: Wrong ID. ");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 540);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
return;
}
else {
analogvalue = Integer.parseInt(status);
pin.setText(status);
final int value = Integer.parseInt(StringValue);
if (analogvalue > value) {
Firebase fireChild = fire2.child(link2 + "/Pin1");
fireChild.setValue(spinner1.getSelectedItem().toString());
Firebase fireChild1 = fire2.child(link2 + "/Pin2");
fireChild1.setValue(spinner2.getSelectedItem().toString());
}
if (analogvalue <= value) {
if(spinner1.getSelectedItem().toString().equals("HIGH")){Firebase fireChild = fire2.child(link2 + "/Pin1"); fireChild.setValue("LOW");}
if(spinner1.getSelectedItem().toString().equals("LOW")) {Firebase fireChild = fire2.child(link2 + "/Pin1"); fireChild.setValue("HIGH");}
if(spinner2.getSelectedItem().toString().equals("HIGH")){Firebase fireChild = fire2.child(link2 + "/Pin2"); fireChild.setValue("LOW");}
if(spinner2.getSelectedItem().toString().equals("LOW")) {Firebase fireChild = fire2.child(link2 + "/Pin2"); fireChild.setValue("HIGH");}
}
}
}
}
I/art: Background sticky concurrent mark sweep GC freed 132932(5MB) AllocSpace objects, 21(744KB) LOS objects, 13% free, 35MB/40MB, paused 1.556ms total 109.059ms
I/art: Background sticky concurrent mark sweep GC freed 94569(3MB) AllocSpace objects, 0(0B) LOS objects, 5% free, 38MB/40MB, paused 8.078ms total 68.990ms
I/art: Background sticky concurrent mark sweep GC freed 101736(3MB) AllocSpace objects, 0(0B) LOS objects, 3% free, 39MB/40MB, paused 5.349ms total 81.087ms
I/art: Background sticky concurrent mark sweep GC freed 48181(1662KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 40MB/40MB, paused 5.361ms total 56.784ms
答案 0 :(得分:1)
您在onClick上做了很多工作,当您单击按钮时,它们会做同样的事情。
它可能导致不必要的内存分配,因此您正在执行很多GC。
我建议您在onClick内部移动一些代码,例如
代码以获取诸如findViewById之类的视图,还获取视图变量
您无需动态更改的Firebase代码
用于实例化和设置烤面包消息的代码