繁体   English   中英

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

[英]Android app is working on phone, but doesnt work on tablet

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()));
}

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

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'
}
((Activity) mContext).runOnUiThread(new Runnable() {
  @Override public void run() {
   sayi1.setText("");
  }
 });

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

解释,您正试图从不同的线程触摸(setText())主线程或 ui 线程的视图

使用 runonUIThread() 并将文本更改 function 与常见的 function 同步。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM