gpt4 book ai didi

java - 线程启动两次

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:06 25 4
gpt4 key购买 nike

我的 onCreate 方法有问题。我已经发现,当我切换到此 Activity 时,onCreate 方法会被调用两次,因此会启动 2 个我不想要的线程。因为线程将坐标发送到 RaspberryPi,而第二个不需要的线程总是发送我不想要的 0 0 0。我似乎无法找到解决这个问题的方法。 。 。如果有人能告诉我解决方法,以便线程仅启动一次,我将不胜感激。

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

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Joystick = (ImageView) findViewById(R.id.Joystick);
Regler = (ImageView) findViewById(R.id.Regler);
buttonFoto = (Button) findViewById(R.id.buttonFoto);
buttonVideo = (Button) findViewById(R.id.buttonVideo);
buttonNeu = (Button) findViewById(R.id.buttonNeu);
buttonSpeichern = (Button) findViewById(R.id.buttonSpeichern);

touchscreenJoystick = (TextView) findViewById(R.id.touchscreenJoystick);
touchscreenJoystick.setOnTouchListener(this);
touchscreenRegler = (TextView) findViewById(R.id.touchscreenRegler);
touchscreenRegler.setOnTouchListener(this);

RL = (RelativeLayout) findViewById(R.id.activity_main);
running = true;
firstTouch = true;

Bild = (WebView) findViewById(R.id.webView);
Bild.loadUrl("http://10.0.0.1:8080/stream");

thread = new Thread(new MainActivity.TransmitThread());
thread.start();
}

编辑:我尝试使用 SaveInstanceState 进行一些操作

    //Same onCreate-stuff as above
if(savedInstanceState == null)
{
thread = new Thread(new MainActivity.TransmitThread());
thread.start();
}

}

@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("message","crashed");
super.onSaveInstanceState(outState);
}

这的作用很奇怪。现在我只有一个线程,在发送坐标一次后立即崩溃。

日志:

我在发送之前放置的Log.i:

I/sent: //X: 0 //Y: 0 //Z: 0

紧随其后的日志

I/System: core_booster, getBoosterConfig = false

编辑2:

我还尝试过在其他时间启动线程。在我的 onTouch 中是这样的:

public boolean onTouch(View v, MotionEvent me)
{
xt = me.getX();
yt = me.getY();
int Aktion = me.getAction();

if(firstTouch)
{

thread = new Thread(new MainActivity.TransmitThread());
thread.start();

firstTouch = false;
}
//other stuff that i need to do here
}

但这与我尝试使用 SaveInstanceState 的结果相同,线程传输一次但不循环。

编辑3:我也许也应该发布我的帖子

class TransmitThread implements Runnable
{
@Override
public void run()
{
while(running)
{
delay();
xss = xs;
yss = ys;
zss = zs;
Log.i("sent","//X: " + xss + " //Y: " + yss + " //Z: " + zss);
transmit();
}
}

public void transmit()
{
try{
socket = new Socket(ServerIP,ServerPort);

OutputStream outputStream = socket.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);

BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

printStream.print(xs + " " + ys + " " + zs);
Akkustand = input.readLine();

handler.sendEmptyMessage(0);

}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}

public void delay(){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

最终编辑:

我设法解决了这个问题。我在发送之前检查该值是否为 0,如果是,则将其更改为 200。在另一端,我检查其是否为 200 并将其更改为 0,并忽略我得到的任何 0。

最佳答案

首先,OnCreate 在 Activity 的每个生命周期中只会被调用一次。然而,有很多情况可能会导致您的 Activity 被终止或恢复。因此,OnCreate 再次被调用。

首先你可以调试代码并找到它重复调用的地方。

注意:为了避免这种情况,您可以在 onSaveInstanceState 中保存状态信息,并将其恢复到创建时进入的状态包中。

谢谢

关于java - 线程启动两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002927/

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