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,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,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'
}