gpt4 book ai didi

java - 如何从另一个类打印变量

转载 作者:行者123 更新时间:2023-12-01 06:06:27 25 4
gpt4 key购买 nike

我需要在 onCreate() 方法中打印到 TextView 中的可变电压和电流。电流和电压变量位于 clientgetui() 方法中。

这是我的代码:

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;

public class getUI extends AppCompatActivity {

String IP;
TextView txtCurrent, textVoltage;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_ui);

IP = getIntent().getExtras().getString("IP");





TextView txtCurrent = (TextView) findViewById(R.id.textViewCurrent);
TextView textVoltage = (TextView) findViewById(R.id.textViewVoltage);

// txtCurrent.setText(String.valueOf(current));

Button buttongetvalue = (Button) findViewById(R.id.buttongetUI);
buttongetvalue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
new AsynctaskgetUI().execute();
}catch (Exception e){

}


}
});


} //ONCREATE!



public class AsynctaskgetUI extends AsyncTask<Void, Integer, Void> {


public int mVoltage;
public int mCurrent;

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}

@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);

}

@Override
protected Void doInBackground(Void... voids) {
try {
clientgetui();
} catch (IOException e) {
e.printStackTrace();
};

return null;
}

//____________________CLIENT1_____________________________

public void clientgetui() throws IOException {

DatagramSocket ds = new DatagramSocket();
byte b[] = new byte[]{0x7F, 0x03, 0x01, 0x00, 0x01, 0x15, 0x00, 0x00, 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00};


// byte[] ipA = new byte[]{(byte) 192, (byte) 168, 1, (byte) 100};
// IPAdress = String.valueOf(getIntent().getExtras().getByte("IP insert"));


// String ipA = IP;

InetAddress ia = InetAddress.getByName(IP);

DatagramPacket dp = new DatagramPacket(b, 28, ia, 21234);
ds.send(dp);

byte buffer[] = new byte[28];
DatagramPacket reply = new DatagramPacket(buffer, buffer.length);


ds.receive(reply);

ds.close();

byte[] dp23 = Arrays.copyOfRange(buffer, 8, 12);

int voltage = (dp23[0] * 256 + dp23[1]) & 0xFF; // PRINT VOLTAGE
int current = (dp23[2] * 256 + dp23[3]) & 0xFF; // PRINT CURRENT in TextView


}



}

}

最佳答案

当您使用 AsyncTask

所有 UI 的数据绑定(bind)都是在 onPostExecute 方法中完成的。

 @Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
txtCurrent.setText(String.valueof(current));
txtVoltage.setText(String.valueof(voltage));
}

关于java - 如何从另一个类打印变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43536518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com