gpt4 book ai didi

java - Android 应用程序可以在手机上运行,​​但不能在平板电脑上运行

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

private final void func5() {
final Timer tmer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
while (isRunning) {
Random r = new Random();
rakam = dizi[r.nextInt(5)];
if (a >= 0 & a < 5) {
sayi1.setText(rakam);
sayi_dizi1[a] = sayi1.getText().toString();
System.out.println(sayi_dizi1[a]);
a++;
}
try {
Thread.sleep(Integer.parseInt(timer2.getText().toString()));
sayi1.setText("");
} catch (InterruptedException e) {
e.printStackTrace();
}
if (a >= 5) {
sayi1.setText("");
sayi2.setText(rakam);
sayi_dizi1[a] = sayi2.getText().toString();
System.out.println(sayi_dizi1[a]);
a++;
b++;
}
try {
Thread.sleep(Integer.parseInt(timer2.getText().toString()));
sayi2.setText("");
} catch (InterruptedException e) {
e.printStackTrace();
}
if (b >= 5) {
isRunning=false;
tmer.cancel();
sayi1.setText("");
sayi2.setText("");
}
}
}
};
tmer.schedule(task, 0, Integer.parseInt(timer.getText().toString()));
}

当我按下按钮时,该功能正在运行。该应用程序可以在手机上运行,​​但不能在平板电脑上运行。我不明白。造成这种情况的原因可能是线程太多。我的平板电脑是旧版本。 (4.4.3 奇巧)。我在 Android Studio 模拟器上设置了另一个版本。这是最后一个版本。并且应用程序可以在模拟器上运行。为什么会出现这种情况呢?这是运行输出:

E/AndroidRuntime: FATAL EXCEPTION: Timer-0
Process: com.example.hafza, PID: 9621
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6669)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1005)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4548)
at android.view.View.invalidate(View.java:11095)
at android.view.View.invalidate(View.java:11044)
at android.widget.TextView.checkForRelayout(TextView.java:6768)
at android.widget.TextView.setText(TextView.java:3850)
at android.widget.TextView.setText(TextView.java:3708)
at android.widget.TextView.setText(TextView.java:3683)
at com.example.hafza.MainActivity$1.run(MainActivity.java:43)
at java.util.Timer$TimerImpl.run(Timer.java:284)
D/OpenGLRenderer: prepareDirty (0.00, 0.00, 600.00, 976.00) opaque 1 <0x5fee5ce8>
D/OpenGLRenderer: finish <0x5fee5ce8>
D/OpenGLRenderer: prepareDirty (0.00, 0.00, 600.00, 976.00) opaque 1 <0x5fee5ce8>
D/OpenGLRenderer: finish <0x5fee5ce8>
D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{41cfcb20 token=android.os.BinderProxy@41cfc2d0 {com.example.hafza/com.example.hafza.MainActivity}}
D/ActivityThread: ACT-PAUSE_ACTIVITY_FINISHING handled : 0 / android.os.BinderProxy@41cfc2d0
D/OpenGLRenderer: Flushing caches (mode 0)
D/GraphicBuffer: close handle(0x5fee5be8) (w:976 h:600 f:1)
D/GraphicBuffer: close handle(0x604a7d78) (w:976 h:600 f:1)
D/GraphicBuffer: close handle(0x60484a08) (w:976 h:600 f:1)
close handle(0x60480d58) (w:976 h:600 f:1)
D/OpenGLRenderer: Flushing caches (mode 1)
D/OpenGLRenderer: Flushing caches (mode 0)
D/ActivityThread: ACT-DESTROY_ACTIVITY handled : 1 / android.os.BinderProxy@41cfc2d0

这是我的 gradle 文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 29

buildToolsVersion '29.0.2'
defaultConfig {
applicationId "com.example.hafza"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation 'com.android.support:multidex:1.0.3'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

最佳答案

使用runonUIThread(),将文本更改功能与普通功能同步。

关于java - Android 应用程序可以在手机上运行,​​但不能在平板电脑上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58879790/

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