我的onCreate方法有问题。我已经确定,当我切换到这个活动时,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/System: core_booster, getBoosterConfig = false
编辑2
I/sent: //X: 0 //Y: 0 //Z: 0
登录也尝试在另一个时间启动线程。在我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。
使用布尔标志 –
@Slimestone尝试调试您的代码并找到它。这很简单。您甚至可以检查该变量是否包含0,并在发送之前避免它。 – RameshJaga
你最终可以使用'thread.isAlive()'https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isAlive(),但你必须检查'线程! = null' –