【问题标题】:simple android program not working as desired简单的 android 程序无法按预期工作
【发布时间】:2018-01-31 15:38:26
【问题描述】:

期望:

如果 'edittext' 有一些文本,则在 1 秒后将其复制到剪贴板。 按下“按钮”时也会复制到剪贴板。

现实:

等待键入的文本被复制到剪贴板但没有发生, 按下按钮时复制效果很好。

代码:

包 com.quickclip.panky.quickclip;

导入 android.content.ClipData;进口 android.content.ClipboardManager;导入android.content.Context; 导入android.support.v7.app.AppCompatActivity;进口 android.os.Bundle;导入android.view.View;进口 android.widget.Button;导入android.widget.EditText;

公共类 MainActivity 扩展 AppCompatActivity 实现 View.OnClickListener {

EditText e1;
Button b1;

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

    e1 = (EditText) findViewById(R.id.editText);
    b1 = (Button) findViewById(R.id.button);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) { copyMethod(); }
    });

    Runnable myRunnable = new Runnable() {
        @Override
        public void run() {
            while (true) {
                try { Thread.sleep(1000); }
                catch (InterruptedException e) { e.printStackTrace(); }
                Runnable newRunnable = new Runnable() {
                    @Override
                    public void run() { copyMethod(); }
                };
            }
        }
    };

    Thread myThread = new Thread(myRunnable);
    myThread.start();
}

public void copyText(View view) { copyMethod(); }

public void copyMethod()
{
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("Copied Text", (CharSequence) e1.getText().toString());
        if (clipboard != null) { clipboard.setPrimaryClip(clip); }
        e1.setText("");
}

@Override
public void onClick(View view) { copyMethod(); } }

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.quickclip.panky.quickclip.MainActivity">


    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:ems="100"
        android:inputType="textAutoCorrect" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:onClick="copyText"
        android:text="Copy" />

</android.widget.RelativeLayout>

Logcat:

01-31 20:58:49.951 11883-11883/com.quickclip.panky.quickclip I/art: Late-enabling -Xcheck:jni
01-31 20:58:50.262 11883-11883/com.quickclip.panky.quickclip W/System: ClassLoader referenced unknown path: /data/app/com.quickclip.panky.quickclip-1/lib/arm64
01-31 20:58:50.264 11883-11883/com.quickclip.panky.quickclip D/ActivityThread: installProvider: context.getPackageName()=com.quickclip.panky.quickclip
01-31 20:58:50.267 11883-11883/com.quickclip.panky.quickclip I/InstantRun: starting instant run server: is main process
01-31 20:58:50.398 11883-11883/com.quickclip.panky.quickclip W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-31 20:58:50.447 11883-11883/com.quickclip.panky.quickclip D/PhoneWindow: DEBUG_SYSTEMUI:origin statusbar style 
01-31 20:58:50.447 11883-11883/com.quickclip.panky.quickclip D/PhoneWindow: DEBUG_SYSTEMUI:windowDrawsFlag set
01-31 20:58:50.447 11883-11883/com.quickclip.panky.quickclip D/PhoneWindow: DEBUG_SYSTEMUI:IconColor=1
01-31 20:58:50.447 11883-11883/com.quickclip.panky.quickclip D/PhoneWindow: DEBUG_SYSTEMUI:StatusBarColor final set ff303f9f
01-31 20:58:50.631 11883-11883/com.quickclip.panky.quickclip V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.PhoneWindow$DecorView{19bf69f I.E...... R.....ID 0,0-0,0}
01-31 20:58:50.637 11883-11883/com.quickclip.panky.quickclip D/WindowClient: Add to mViews: com.android.internal.policy.PhoneWindow$DecorView{19bf69f I.E...... R.....ID 0,0-0,0}, this = android.view.WindowManagerGlobal@143e3bd
01-31 20:58:50.638 11883-11883/com.quickclip.panky.quickclip D/OpenGLRenderer: Dumper init 4 threads <0x7fa0c7f5c0>
01-31 20:58:50.638 11883-11883/com.quickclip.panky.quickclip D/OpenGLRenderer: <com.quickclip.panky.quickclip> is running.
01-31 20:58:50.640 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-31 20:58:50.641 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: CanvasContext() 0x7fa3a46000
01-31 20:58:50.644 11883-11883/com.quickclip.panky.quickclip D/ViewRootImpl: hardware acceleration is enabled, this = ViewRoot{a79bdb2 com.quickclip.panky.quickclip/com.quickclip.panky.quickclip.MainActivity,ident = 0}
01-31 20:58:50.648 11883-11883/com.quickclip.panky.quickclip V/PhoneWindow: DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{a79bdb2 com.quickclip.panky.quickclip/com.quickclip.panky.quickclip.MainActivity,ident = 0}, this =com.android.internal.policy.PhoneWindow$DecorView{19bf69f V.E...... R.....ID 0,0-0,0}
01-31 20:58:50.690 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: CanvasContext() 0x7fa3a46000 initialize window=0x7fac9df010, title=com.quickclip.panky.quickclip/com.quickclip.panky.quickclip.MainActivity
01-31 20:58:50.690 11883-11883/com.quickclip.panky.quickclip D/Surface: Surface::allocateBuffers(this=0x7fac9df000)
01-31 20:58:50.696 11883-11902/com.quickclip.panky.quickclip I/OpenGLRenderer: Initialized EGL, version 1.4
01-31 20:58:50.699 11883-11902/com.quickclip.panky.quickclip D/MALI: gles_context_new:265: [MTK] sufficient memory..
01-31 20:58:50.699 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: Created EGL context (0x7fa4672f80)
01-31 20:58:50.701 11883-11902/com.quickclip.panky.quickclip I/OpenGLRenderer: Get enable program binary service property (1)
01-31 20:58:50.701 11883-11902/com.quickclip.panky.quickclip I/OpenGLRenderer: Initializing program atlas...
01-31 20:58:50.702 11883-11902/com.quickclip.panky.quickclip D/ProgramBinary/Service: BpProgramBinaryService.getFileDescriptor
01-31 20:58:50.703 11883-11902/com.quickclip.panky.quickclip D/ProgramBinary/Service: BpProgramBinaryService.getProgramMapLen
01-31 20:58:50.703 11883-11902/com.quickclip.panky.quickclip D/ProgramBinary/Service: BpProgramBinaryService.getProgramMapArray
01-31 20:58:50.703 11883-11902/com.quickclip.panky.quickclip D/ProgramBinary/Service: BpProgramBinaryService.getProgramBinaryLen
01-31 20:58:50.704 11883-11902/com.quickclip.panky.quickclip I/OpenGLRenderer: Program binary detail: Binary length is 173520, program map length is 152.
01-31 20:58:50.704 11883-11902/com.quickclip.panky.quickclip I/OpenGLRenderer: Succeeded to mmap program binaries. File descriptor is 47, and path is /dev/ashmem.
01-31 20:58:50.704 11883-11902/com.quickclip.panky.quickclip I/OpenGLRenderer: No need to use file discriptor anymore, close fd(47).
01-31 20:58:50.704 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: Initializing program cache from 0x7fb1123bc8, size = 6
01-31 20:58:50.707 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: -- init (key = 0x0000000000000000)
01-31 20:58:50.707 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: -- not init (key = 0x0000000000500040)
01-31 20:58:50.708 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: -- init (key = 0x0000000000500041)
01-31 20:58:50.710 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: -- init (key = 0x0000000800000003)
01-31 20:58:50.711 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: -- init (key = 0x0000001000500040)
01-31 20:58:50.713 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: -- init (key = 0x0000003800000000)
01-31 20:58:50.713 11883-11902/com.quickclip.panky.quickclip D/Surface: Surface::connect(this=0x7fac9df000,api=1)
01-31 20:58:50.714 11883-11902/com.quickclip.panky.quickclip W/libEGL: [ANDROID_RECORDABLE] format: 1
01-31 20:58:50.715 11883-11902/com.quickclip.panky.quickclip D/mali_winsys: new_window_surface returns 0x3000
01-31 20:58:50.747 11883-11883/com.quickclip.panky.quickclip W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
01-31 20:58:50.779 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: CacheTexture 2 upload: x, y, width height = 0, 0, 97, 148
01-31 20:58:50.786 11883-11902/com.quickclip.panky.quickclip I/[MALI][Gralloc]: [+]r_hnd(0x7fa4675440), client(51), share_fd(49)
01-31 20:58:50.786 11883-11902/com.quickclip.panky.quickclip D/GraphicBuffer: register, handle(0x7fa4675440) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
01-31 20:58:50.790 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: ProgramCache save to disk, size = 6
01-31 20:58:50.801 11883-11902/com.quickclip.panky.quickclip I/[MALI][Gralloc]: [+]r_hnd(0x7fa4675120), client(51), share_fd(53)
01-31 20:58:50.801 11883-11902/com.quickclip.panky.quickclip D/GraphicBuffer: register, handle(0x7fa4675120) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
01-31 20:58:50.805 11883-11883/com.quickclip.panky.quickclip V/InputMethodManager: onWindowFocus: android.support.v7.widget.AppCompatEditText{23f6523 VFED..CL. .F....ID 0,60-720,151 #7f070030 app:id/editText} softInputMode=288 first=true flags=#81810100
01-31 20:58:50.806 11883-11893/com.quickclip.panky.quickclip I/System: FinalizerDaemon: finalize objects = 1
01-31 20:58:50.808 11883-11883/com.quickclip.panky.quickclip V/InputMethodManager: START INPUT: android.support.v7.widget.AppCompatEditText{23f6523 VFED..CL. .F....ID 0,60-720,151 #7f070030 app:id/editText} ic=com.android.internal.widget.EditableInputConnection@b1db6e0 tba=android.view.inputmethod.EditorInfo@a9f4d99 controlFlags=#107
01-31 20:58:50.825 11883-11902/com.quickclip.panky.quickclip I/[MALI][Gralloc]: [+]r_hnd(0x7fa46754e0), client(51), share_fd(56)
01-31 20:58:50.825 11883-11902/com.quickclip.panky.quickclip D/GraphicBuffer: register, handle(0x7fa46754e0) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
01-31 20:59:20.710 11883-11883/com.quickclip.panky.quickclip D/TextView: touchFinish handled:true    --- mShowToolbar=true
01-31 20:59:22.042 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: CacheTexture 2 upload: x, y, width height = 0, 147, 18, 29
01-31 20:59:22.340 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: CacheTexture 2 upload: x, y, width height = 0, 175, 20, 23
01-31 20:59:23.804 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: CacheTexture 2 upload: x, y, width height = 0, 197, 19, 30
01-31 20:59:24.070 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: CacheTexture 2 upload: x, y, width height = 64, 62, 6, 29
01-31 20:59:24.514 11883-11902/com.quickclip.panky.quickclip D/OpenGLRenderer: CacheTexture 2 upload: x, y, width height = 0, 226, 19, 23
01-31 20:59:33.248 11883-11883/com.quickclip.panky.quickclip I/View: Key down dispatch to android.support.v7.widget.AppCompatEditText{23f6523 VFED..CL. .F...... 0,60-720,151 #7f070030 app:id/editText}, event = KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ENTER, scanCode=0, metaState=0, flags=0x16, repeatCount=0, eventTime=44923912, downTime=44923912, deviceId=-1, source=0x0 }
01-31 20:59:33.252 11883-11883/com.quickclip.panky.quickclip V/ViewRootImpl: [ANR Warning]Input routeing takes more than 6000ms since 1970-01-01 05:30:00.000, this = ViewRoot{a79bdb2 com.quickclip.panky.quickclip/com.quickclip.panky.quickclip.MainActivity,ident = 0}
01-31 20:59:33.252 11883-11883/com.quickclip.panky.quickclip V/ViewRootImpl: Input event delivered to android.view.ViewRootImpl$EarlyPostImeInputStage@76d1217 at 2018-01-31 20:59:33.244
01-31 20:59:33.253 11883-11883/com.quickclip.panky.quickclip V/ViewRootImpl: Input event delivered to android.view.ViewRootImpl$NativePostImeInputStage@9bde604 at 2018-01-31 20:59:33.245
01-31 20:59:33.253 11883-11883/com.quickclip.panky.quickclip V/ViewRootImpl: Input event delivered to android.view.ViewRootImpl$ViewPostImeInputStage@d0857ed at 2018-01-31 20:59:33.245
01-31 20:59:33.253 11883-11883/com.quickclip.panky.quickclip V/ViewRootImpl: Input event delivered to android.view.ViewRootImpl$SyntheticInputStage@5a5d622 at 2018-01-31 20:59:33.250
01-31 20:59:33.255 11883-11883/com.quickclip.panky.quickclip I/View: Key up dispatch to android.support.v7.widget.AppCompatEditText{23f6523 VFED..CL. .F...... 0,60-720,151 #7f070030 app:id/editText}, event = KeyEvent { action=ACTION_UP, keyCode=KEYCODE_ENTER, scanCode=0, metaState=0, flags=0x16, repeatCount=0, eventTime=44923912, downTime=44923912, deviceId=-1, source=0x0 }
01-31 20:59:33.267 11883-11883/com.quickclip.panky.quickclip V/ViewRootImpl: [ANR Warning]Input routeing takes more than 6000ms since 1970-01-01 05:30:00.000, this = ViewRoot{a79bdb2 com.quickclip.panky.quickclip/com.quickclip.panky.quickclip.MainActivity,ident = 0}
01-31 20:59:33.267 11883-11883/com.quickclip.panky.quickclip V/ViewRootImpl: Input event delivered to android.view.ViewRootImpl$EarlyPostImeInputStage@76d1217 at 2018-01-31 20:59:33.253
01-31 20:59:33.267 11883-11883/com.quickclip.panky.quickclip V/ViewRootImpl: Input event delivered to android.view.ViewRootImpl$NativePostImeInputStage@9bde604 at 2018-01-31 20:59:33.254
01-31 20:59:33.268 11883-11883/com.quickclip.panky.quickclip V/ViewRootImpl: Input event delivered to android.view.ViewRootImpl$ViewPostImeInputStage@d0857ed at 2018-01-31 20:59:33.254
01-31 20:59:33.268 11883-11883/com.quickclip.panky.quickclip V/ViewRootImpl: Input event delivered to android.view.ViewRootImpl$SyntheticInputStage@5a5d622 at 2018-01-31 20:59:33.266
01-31 20:59:33.268 11883-11883/com.quickclip.panky.quickclip E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
01-31 20:59:33.270 11883-11883/com.quickclip.panky.quickclip E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

【问题讨论】:

    标签: java android


    【解决方案1】:

    您应该从主线程访问任何 UI 组件,其中

    如果你的活动,你分配实例的活动变量

     Activity activity = this; 
    

    所以修改后的 Runnable 应该是这样的。

    Runnable myRunnable = new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try { Thread.sleep(1000); }
                        catch (InterruptedException e) { e.printStackTrace(); }
                        activity.runOnUiThread(new Runnable() {
                            public void run() {
                                copyMethod();
                            }
                        });
                    }
                }
            };
    

    【讨论】:

    • 这给出了不兼容的类型
    • @PankajKumar 您能否指定更多详细信息,您在哪个位置获得不兼容的类型?这条线你得到不兼容的类型吗?活动活动=这个;
    • 它不应该只是在它的使用之上,而是在 myRunnable 之外,即在 Runnable 之前 myRunnable = new Runnable() {}
    【解决方案2】:

    https://developer.android.com/training/multiple-threads/define-runnable.html

    在类中,Runnable.run() 方法包含以下代码 执行。通常,在 Runnable 中允许任何内容。记住, 但是,Runnable 不会在 UI 线程上运行,所以它 不能直接修改View对象等UI对象。

    你不能在Runnable.run()中做e1.setText("");

    你应该使用AsyncTask

     private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
         protected Void doInBackground(Void... void) {
             try { Thread.sleep(1000); }
             catch (InterruptedException e) { e.printStackTrace(); }
         }
    
         protected void onPostExecute(Void result) {
             e1.setText("");
         }
     }
    

    https://developer.android.com/reference/android/os/AsyncTask.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      相关资源
      最近更新 更多