简体   繁体   English

OnPrimaryClipChangedListener()仅可使用一次

[英]OnPrimaryClipChangedListener() works only for once

I'm trying to make a application which toast the word you copied to the clipboard in a WebView for practice. 我正在尝试制作一个应用程序,该应用程序将您复制到WebView中的剪贴板的单词敬酒以进行练习。 The problem is, this code works for the first word(except somehow it toasts for three times. Sort of blink.); 问题是,此代码适用于第一个单词(除了以某种方式敬酒了三遍。有点眨眼)。 however, when I try to copy a word for the second time, it shows "Unfortunately, (my app's name) has stopped" and stop working. 但是,当我第二次尝试复制单词时,它显示"Unfortunately, (my app's name) has stopped"并停止工作。 It still shows the website, but the toast function stops. 它仍然显示网站,但Toast功能停止。 I'm running this program on an emulator and using Eclipse. 我正在模拟器上运行该程序,并使用Eclipse。 Following Mr/Ms.jgriffsta's advise, I added a try&catch statement, then for the second time it toasts "Error occured" once, and then toasts the copied word for two times. 按照jgriffsta先生的建议,我添加了一条try&catch语句,然后第二次将“发生错误”举杯一次,然后将复制的单词举杯两次。 Again sort of blink. 再次眨眼。

//ItemDetailActivity.java
public class ItemDetailActivity extends Activity {
    private WebView mWeb;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_detail);

        Intent intent = getIntent();

        String link = intent.getStringExtra("LINK");
        mWeb = (WebView)findViewById(R.id.web);
        mWeb.loadUrl(link);

        final ClipboardManager cm = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
        cm.addPrimaryClipChangedListener(new OnPrimaryClipChangedListener(){
            @Override
            public void onPrimaryClipChanged(){
                ClipData.Item item = cm.getPrimaryClip().getItemAt(0);
                String text = item.getText().toString();
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

----LogCat-------- ---- logcat的--------

07-18 18:15:07.530: E/chromium(837): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
07-18 18:15:07.540: E/chromium(837): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
07-18 18:15:07.540: E/chromium(837): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
07-18 18:15:07.540: E/chromium(837): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
07-18 18:15:07.540: E/chromium(837): [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed
07-18 18:15:10.360: E/chromium(837): [ERROR:simple_backend_impl.cc(186)] File structure does not match the disk cache backend.
07-18 18:15:10.360: E/chromium(837): [ERROR:simple_backend_impl.cc(402)] Simple Cache Backend: wrong file structure on disk: /data/data/com.kariga.rssreader1/app_webview/Cache
07-18 18:16:03.910: E/AndroidRuntime(837): FATAL EXCEPTION: main
07-18 18:16:03.910: E/AndroidRuntime(837): Process: com.kariga.rssreader1, PID: 837
07-18 18:16:03.910: E/AndroidRuntime(837): java.lang.NullPointerException
07-18 18:16:03.910: E/AndroidRuntime(837):  at com.kariga.rssreader1.ItemDetailActivity$1.onPrimaryClipChanged(ItemDetailActivity.java:34)
07-18 18:16:03.910: E/AndroidRuntime(837):  at android.content.ClipboardManager.reportPrimaryClipChanged(ClipboardManager.java:234)
07-18 18:16:03.910: E/AndroidRuntime(837):  at android.content.ClipboardManager$2.handleMessage(ClipboardManager.java:76)
07-18 18:16:03.910: E/AndroidRuntime(837):  at android.os.Handler.dispatchMessage(Handler.java:102)
07-18 18:16:03.910: E/AndroidRuntime(837):  at android.os.Looper.loop(Looper.java:136)
07-18 18:16:03.910: E/AndroidRuntime(837):  at android.app.ActivityThread.main(ActivityThread.java:5017)
07-18 18:16:03.910: E/AndroidRuntime(837):  at java.lang.reflect.Method.invokeNative(Native Method)
07-18 18:16:03.910: E/AndroidRuntime(837):  at java.lang.reflect.Method.invoke(Method.java:515)
07-18 18:16:03.910: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-18 18:16:03.910: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-18 18:16:03.910: E/AndroidRuntime(837):  at dalvik.system.NativeStart.main(Native Method)

----Edited Code with Try&Catch Statement-------- ----使用Try&Catch语句编辑代码--------

final ClipboardManager cm = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
cm.addPrimaryClipChangedListener(new OnPrimaryClipChangedListener(){
    @Override
    public void onPrimaryClipChanged(){
        ClipData.Item item = cm.getPrimaryClip().getItemAt(0);
        try{String text = item.getText().toString();
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
        }
        catch(Exception e){
            Toast.makeText(getApplicationContext(), "Error occured", Toast.LENGTH_SHORT).show();
        }
    }
});

----LogCat after edition-------- ---- LogCat版本之后--------

07-18 18:57:28.780: E/chromium(882): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
07-18 18:57:28.780: E/chromium(882): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
07-18 18:57:28.790: E/chromium(882): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
07-18 18:57:28.790: E/chromium(882): [ERROR:gl_surface_egl.cc(620)]  GLSurfaceEGL::InitializeOneOff failed.
07-18 18:57:28.790: E/chromium(882): [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed

(Would Put this as comment if I could) (如果可以的话,将其作为评论)

You don't check for any exceptions, so I would put a try, catch statment on 您不检查任何例外情况,所以我会尝试一下,在上面声明

String text = item.getText().toString(); 

Could you post the error report to the logcat. 您可以将错误报告发布到logcat吗? It should point out the exact reason. 应该指出确切原因。

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

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