Android应用程序可在手机上运行, 但不适用于平板电脑

问题描述 投票:0回答:2
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'
}
java android android-emulator
2个回答
0
投票
((Activity) mContext).runOnUiThread(new Runnable() {
  @Override public void run() {
   sayi1.setText("");
  }
 });

这样做(其中mContext =具有sayi1视图的活动的上下文):

说明,您试图触摸(setText())来自其他线程的主线程或ui线程的视图


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.