0

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
  • Do you mean that you cannot see your activity? or do you get an exception? – hoomi Aug 30 '14 at 08:56
  • The App is not Openingn at all –  Aug 30 '14 at 09:30
  • @hoomi ,i already give the Tutorial Link as well –  Aug 30 '14 at 09:32
  • The thing is I am not an editor of a blog. I am here to answer your question :). Anyways if the app is not openning at all it means that you have a problem in your manifest. Can you provide your manifest file please? – hoomi Aug 30 '14 at 09:36
  • i updated the question with manifest –  Aug 30 '14 at 09:42
  • Your manifest looks fine. Have you tried commenting everything on `OnCreate` except for `setContentView` just to make sure that your app opens with a UI – hoomi Aug 30 '14 at 09:46
  • i added the log cat error as well –  Aug 30 '14 at 09:51
  • So you do get an exception. The log message that you added should have a line which says the type of exception. Can you also add that? – hoomi Aug 30 '14 at 09:56
  • in this much time you should try this on your own system rather than asking me ? –  Aug 30 '14 at 10:11
  • I hope that you understand I am not only answering your question. I cannot spend time and start a new project for each question.Even if I run it on my machine I cannot know why you are facing that issue. You need to be more informative in your questions if you want answers – hoomi Aug 30 '14 at 10:19
  • @hoomi have a look i update the full Logcat –  Aug 30 '14 at 10:43
  • What version of Android is on your phone/simulator `android.content.ClipboardManager` is available on API V11+ – hoomi Aug 30 '14 at 10:48
  • i am running Android 2.2 on emulator –  Aug 30 '14 at 10:49

1 Answers1

0

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

hoomi
  • 1,882
  • 2
  • 16
  • 17
  • but getPrimaryClip() and setPrimaryClip() methods not working with this import. –  Aug 30 '14 at 10:59
  • Have a look at the doc http://developer.android.com/reference/android/text/ClipboardManager.html – hoomi Aug 30 '14 at 11:07
  • 1
    For android.text.ClipboardManager you have to use `setText` and `getText` instead of `setPrimaryClip` and `getPrimaryClip` – hoomi Aug 30 '14 at 11:08