0

尝试将 TextView 文本复制到剪贴板时,我做错了吗?

我的活动课程中的代码:

@Override
public boolean onContextItemSelected(MenuItem item) {
    String selectedItem = item.getTitle().toString().trim();
    try {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip;
    if (selectedItem.equals("Copy Query")) {
        clip = ClipData.newPlainText("Query",tv1.getText());
    clipboard.setPrimaryClip(clip);
    }           
    return true;
}

错误:

04-17 03:10:54.056: E/AndroidRuntime(503): FATAL EXCEPTION: main
04-17 03:10:54.056: E/AndroidRuntime(503): java.lang.NoClassDefFoundError: android.content.ClipboardManager
04-17 03:10:54.056: E/AndroidRuntime(503):  at com.ewe.radixcalculator.CalculatorActivity.onContextItemSelected(CalculatorActivity.java:68)
04-17 03:10:54.056: E/AndroidRuntime(503):  at android.app.Activity.onMenuItemSelected(Activity.java:2199)
04-17 03:10:54.056: E/AndroidRuntime(503):  at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2744)
04-17 03:10:54.056: E/AndroidRuntime(503):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
04-17 03:10:54.056: E/AndroidRuntime(503):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
04-17 03:10:54.056: E/AndroidRuntime(503):  at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
04-17 03:10:54.056: E/AndroidRuntime(503):  at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:874)
04-17 03:10:54.056: E/AndroidRuntime(503):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-17 03:10:54.056: E/AndroidRuntime(503):  at android.widget.ListView.performItemClick(ListView.java:3382)
04-17 03:10:54.056: E/AndroidRuntime(503):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
04-17 03:10:54.056: E/AndroidRuntime(503):  at android.os.Handler.handleCallback(Handler.java:587)
04-17 03:10:54.056: E/AndroidRuntime(503):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 03:10:54.056: E/AndroidRuntime(503):  at android.os.Looper.loop(Looper.java:123)
04-17 03:10:54.056: E/AndroidRuntime(503):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-17 03:10:54.056: E/AndroidRuntime(503):  at java.lang.reflect.Method.invokeNative(Native Method)
04-17 03:10:54.056: E/AndroidRuntime(503):  at java.lang.reflect.Method.invoke(Method.java:521)
04-17 03:10:54.056: E/AndroidRuntime(503):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-17 03:10:54.056: E/AndroidRuntime(503):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-17 03:10:54.056: E/AndroidRuntime(503):  at dalvik.system.NativeStart.main(Native Method)
04-17 03:10:54.056: E/AndroidRuntime(503): Caused by: java.lang.ClassNotFoundException: android.content.ClipboardManager in loader dalvik.system.PathClassLoader[/data/app/com.ewe.radixcalculator-2.apk]
04-17 03:10:54.056: E/AndroidRuntime(503):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
04-17 03:10:54.056: E/AndroidRuntime(503):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-17 03:10:54.056: E/AndroidRuntime(503):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
4

1 回答 1

9

从您的堆栈跟踪中,很明显ClipboardManager您正在使用未找到。快速查找显示ClipboardManager,Android 中有两个。

  1. ClipboardManager(android.text.ClipboardManager) 自 API 1
  2. ClipboardManager(android.content.ClipboardManager) 自 API 11

根据您的实现,我猜您打算使用仅包含在 API 11 中的后者。因此,您的代码会导致错误。

Eclipse 不会向您显示错误,因为您已android.text.ClipboardManager在导入部分中导入。

要解决此问题,请导入正确的包并指向相关的 API 版本。

如果您想android.text.ClipboardManager用于旧版本,我认为应该没问题。这是我将如何做到的。希望这可以帮助。

@Override
public boolean onContextItemSelected(MenuItem item) {
    String selectedItem = item.getTitle().toString().trim();
    try {
    ClipboardManager clipboard = new ClipboardManager();
    if (selectedItem.equals("Copy Query")) {
        clipboard.setText(tv1.getText().toString());
    }           
    return true;
}
于 2012-04-17T04:16:14.783 回答