我有简单的剪贴板管理器活动,它将EditText上的数据复制并粘贴到下一个EditText框但是活动未启动
我的 MainActivity 如下所示:
package com.example.clipboarddemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private ClipboardManager mClipboardManager;
private ClipData myClip;
private EditText copyField,pasteField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClipboardManager=(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
copyField=(EditText)findViewById(R.id.editText1);
pasteField=(EditText)findViewById(R.id.editText2);
}
@SuppressLint("NewApi")
public void copy(View view){
String text=copyField.getText().toString();
myClip=ClipData.newPlainText("text", text);
mClipboardManager.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Text Copied",Toast.LENGTH_SHORT).show();
}
@SuppressLint("NewApi")
public void paste(View view){
ClipData abc=mClipboardManager.getPrimaryClip();
ClipData.Item item=abc.getItemAt(0);
String text=item.getText().toString();
pasteField.setText(text);
Toast.makeText(getApplicationContext(),"Text Pasted",Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
和 AndroidManifest.xml 文件是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clipboarddemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我已阅读以下教程创建此课程:
Logcat错误显示如下:
08-30 16:09:23.810: E/AndroidRuntime(296): FATAL EXCEPTION: main
08-30 16:09:23.810: E/AndroidRuntime(296): java.lang.NoClassDefFoundError: android.content.ClipboardManager
08-30 16:09:23.810: E/AndroidRuntime(296): at com.example.clipboarddemo.MainActivity.onCreate(MainActivity.java:26)
08-30 16:09:23.810: E/AndroidRuntime(296): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-30 16:09:23.810: E/AndroidRuntime(296): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-30 16:09:23.810: E/AndroidRuntime(296): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-30 16:09:23.810: E/AndroidRuntime(296): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-30 16:09:23.810: E/AndroidRuntime(296): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-30 16:09:23.810: E/AndroidRuntime(296): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 16:09:23.810: E/AndroidRuntime(296): at android.os.Looper.loop(Looper.java:123)
08-30 16:09:23.810: E/AndroidRuntime(296): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-30 16:09:23.810: E/AndroidRuntime(296): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 16:09:23.810: E/AndroidRuntime(296): at java.lang.reflect.Method.invoke(Method.java:521)
08-30 16:09:23.810: E/AndroidRuntime(296): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-30 16:09:23.810: E/AndroidRuntime(296): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-30 16:09:23.810: E/AndroidRuntime(296): at dalvik.system.NativeStart.main(Native Method)
08-30 16:09:23.870: W/ActivityManager(59): Force finishing activity com.example.clipboarddemo/.MainActivity
08-30 16:09:24.862: W/ActivityManager(59): Activity pause timeout for HistoryRecord{4a2d49e0 com.example.clipboarddemo/.MainActivity}
08-30 16:09:25.060: I/ActivityManager(59): Displayed activity com.android.launcher/com.android.launcher2.Launcher: 44069 ms (total 44069 ms)
08-30 16:09:35.673: W/ActivityManager(59): Activity destroy timeout for HistoryRecord{4a2d49e0 com.example.clipboarddemo/.MainActivity}
08-30 16:10:53.140: D/KeyguardViewMediator(59): pokeWakelock(5000)
08-30 16:10:53.320: D/KeyguardViewMediator(59): pokeWakelock(5000)
08-30 16:10:53.690: W/WindowManager(59): No window to dispatch pointer action 1
08-30 16:10:54.370: I/ARMAssembler(59): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x577a10:0x577acc] in 602296 ns
08-30 16:10:56.640: I/Process(296): Sending signal. PID: 296 SIG: 9
08-30 16:10:56.660: I/ActivityManager(59): Process com.example.clipboarddemo (pid 296) has died.
08-30 16:10:57.070: I/ARMAssembler(59): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x577ad0:0x577c98] in 866708 ns
答案 0 :(得分:0)
由于android.content.ClipboardManager
在Android 3.0.x及更高版本上可用,因此您无法在Android 2.2上运行它。这就是为什么你得到NoClassDefFoundError
例外。
如果您想在Android 2.3.x及更低版本上使用ClipBoard,我建议您使用android.text.ClipboardManager
我希望它有所帮助