Clipboard Manager Activity Not Working in Android

2019-09-06 20:45发布

问题:

I have simple Clipboard Manager Activity Which Copy And Paste The Data From on EditText To next EditText box but The Activity Not Starting

My MainActivity is As Follows:

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

}

and AndroidManifest.xml file is:

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

I have Read the Following Tutorial To Create This Class:

Tutorials Point

Logcat error is showing as follows:

    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

回答1:

Since android.content.ClipboardManager is available on Android 3.0.x and above you cannot run it on Android 2.2. That is why you are getting NoClassDefFoundError exception.

If you want to use ClipBoard on Android 2.3.x and below I suggest that you use android.text.ClipboardManager

I hope that it helps