gpt4 book ai didi

java - OnPrimaryClipChangedListener() 只能工作一次

转载 作者:太空宇宙 更新时间:2023-11-04 14:42:05 26 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,该应用程序可以将您在 Web View 中复制到剪贴板的单词进行烘烤以进行练习。问题是,这段代码适用于第一个单词(除了不知何故它烤了三遍。有点眨眼。);然而,当我第二次尝试复制一个单词时,它显示“不幸的是,(我的应用程序的名称)已停止”并停止工作。它仍然显示网站,但 toast 功能停止。我正在模拟器上运行这个程序并使用 Eclipse。按照 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 语句编辑的代码--------

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

最佳答案

(如果可以的话,我会将其作为评论)

你不检查任何异常,所以我会在

上放置一个 try, catch 语句
String text = item.getText().toString(); 

您能否将错误报告发布到 logcat。应该指出具体原因。

关于java - OnPrimaryClipChangedListener() 只能工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24834731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com