gpt4 book ai didi

java - 如何在Java Android Studio中多次运行代码

转载 作者:行者123 更新时间:2023-12-02 09:10:45 25 4
gpt4 key购买 nike

我是制作 Android 应用程序的新手,我不知道如何多次运行一段代码。这是我的代码:

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.Random;
import android.widget.AbsoluteLayout;
import android.util.*;
import java.util.concurrent.TimeUnit;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

int score = 0;
TextView scoreText;
ImageButton mole;
Random random = new Random();

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

scoreText = findViewById(R.id.scoreCount);
mole = findViewById(R.id.mole);
//I want to run this multiple times
TimeUnit.MILLISECONDS.sleep(random.nextInt(500));
AbsoluteLayout.LayoutParams absParams =
(AbsoluteLayout.LayoutParams) mole.getLayoutParams();

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;


Random r = new Random();

absParams.x = r.nextInt(width);
absParams.y = r.nextInt(height);
mole.setLayoutParams(absParams);
}

@Override
public void onClick(View v) {

}
}

我正在尝试制作一个鼹鼠游戏,其中鼹鼠随机穿过屏幕,你必须点击它才能赚取积分,但问题是,我不知道如何运行

TimeUnit.MILLISECONDS.sleep(random.nextInt(500));
AbsoluteLayout.LayoutParams absParams =
(AbsoluteLayout.LayoutParams) mole.getLayoutParams();

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;


Random r = new Random();

absParams.x = r.nextInt(width);
absParams.y = r.nextInt(height);
mole.setLayoutParams(absParams);

一次又一次。我尝试过这样做

Thread test = new Thread(new Runnable() {
@Override
public void run() {
try {
//I want to run this multiple times
TimeUnit.MILLISECONDS.sleep(500);
AbsoluteLayout.LayoutParams absParams =
(AbsoluteLayout.LayoutParams) mole.getLayoutParams();

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;


Random r = new Random();

absParams.x = r.nextInt(width);
absParams.y = r.nextInt(height);
mole.setLayoutParams(absParams);
}catch(InterruptedException e){
Log.i("Error", "Thread");
}
}
});
test.start();

但它不断返回以下错误:

com.example.test E/AndroidRuntime: FATAL EXCEPTION: Thread-1057
Process: com.example.test, PID: 25335
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7266)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1065)
at android.view.View.requestLayout(View.java:19098)
at android.view.View.requestLayout(View.java:19098)
at android.view.View.requestLayout(View.java:19098)
at android.view.View.requestLayout(View.java:19098)
at android.view.View.requestLayout(View.java:19098)
at android.view.View.requestLayout(View.java:19098)
at android.view.View.requestLayout(View.java:19098)
at android.view.View.setLayoutParams(View.java:12590)
at com.example.test.MainActivity$1.run(MainActivity.java:48)
at java.lang.Thread.run(Thread.java:818)

最佳答案

您可以使用 RxJava 正如 @Mohammad Reza Khahani 提到的那样,这会让生活更轻松,但如果您不想使用它,您有两个选项来运行您的线程并更改 UI。

第一个是创建 Handler 并在线程内部使用它来运行与 UI 相关的代码:

    Handler handler = new Handler(); // create new instance in ui thread eg: @onCreate or @onResume

handler.post(new Runnable() {
@Override
public void run() {
//UI changes
}
});

或者您可以在后台线程内使用 Activity 的 YourActivity.this.runOnUiThread 方法来运行 UI 相关代码。

    runOnUiThread(new Runnable() {
@Override
public void run() {
//UI changes
}
});

关于java - 如何在Java Android Studio中多次运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59441579/

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