OnPrimaryClipChangedListener()仅适用于一次

时间:2014-07-18 22:30:57

标签: java android

我试图制作一个应用程序,它将你复制到Word中的单词干嘛加入WebView进行练习。问题是,这个代码适用于第一个单词(除了它以某种方式敬酒三次。眨眼的类别。);但是,当我尝试第二次复制一个单词时,它会显示出来 "Unfortunately, (my app's name) has stopped" 并停止工作。它仍然显示网站,但吐司功能停止。我在模拟器上运行此程序并使用Eclipse。在Mr / Ms.jgriffsta的建议之后,我添加了一个try& catch语句,然后第二次出现了问候"出现错误"一次,然后将复制的单词两次敬酒。再次眨眼。

//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中

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)

----使用Try& Catch Statement编辑代码--------

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 --------

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

1 个答案:

答案 0 :(得分:0)

(如果可以,请将此作为评论)

你没有检查是否有任何异常,所以我会试一试,抓住

的判断
String text = item.getText().toString(); 

您可以将错误报告发布到logcat吗?它应该指出确切的原因。