Search code examples
androideclipsebuttontextedit

How To Copy a Text from a EditText to another EditText by button clicking in Android?


i wanted to write an app that when you click on the button , copy the text you entered in the EditText to other EditText...

i wrote this codes:

package com.example.learningapp;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button Copy = (Button) findViewById(R.id.button1);
    Copy.setOnClickListener(this);
}


EditText enteredText = (EditText) findViewById(R.id.editText1);
EditText copiedText = (EditText) findViewById(R.id.editText2);


@Override
public void onClick(View v){
    enteredText.getText().toString();
    copiedText.setText((CharSequence) enteredText);  
}
}

and this is the XML file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.learningapp.MainActivity$PlaceholderFragment" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="54dp"
    android:ems="10" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="149dp"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="57dp"
    android:onClick="onClick"
    android:text="Copy" />

 </RelativeLayout>

so this wont run. i get lots of errors i dont know what they are. so somebody help me with it ??

and this is logcat :

08-16 02:58:43.929: E/AndroidRuntime(1860): FATAL EXCEPTION: main
08-16 02:58:43.929: E/AndroidRuntime(1860): Process: com.example.learningapp, PID: 1860   
-16 02:58:43.929: E/AndroidRuntime(1860): java.lang.RuntimeException: Unable to instantiate    activity ComponentInfo{com.example.learningapp/com.example.learningapp.MainActivity}: java.lang.NullPointerException
08-16 02:58:43.929: E/AndroidRuntime(1860):     at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.os.Looper.loop(Looper.java:136)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at java.lang.reflect.Method.invokeNative(Native Method)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at java.lang.reflect.Method.invoke(Method.java:515)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at dalvik.system.NativeStart.main(Native Method)
08-16 02:58:43.929: E/AndroidRuntime(1860): Caused by: java.lang.NullPointerException
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.Activity.findViewById(Activity.java:1884)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at com.example.learningapp.MainActivity.<init>(MainActivity.java:22)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at java.lang.Class.newInstanceImpl(Native Method)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at java.lang.Class.newInstance(Class.java:1208)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-16 02:58:43.929: E/AndroidRuntime(1860):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)

Solution

  • I think this is the problem

    public void onClick(View v){
        enteredText.getText().toString();
        copiedText.setText((CharSequence) enteredText);  
    }
    

    Replace it as

    public void onClick(View v){
    
        copiedText.setText(enteredText.getText().toString(););  
    }
    

    FROM your LOGCAT

    It can be seen that you have put these two lines

    EditText enteredText = (EditText) findViewById(R.id.editText1);
    EditText copiedText = (EditText) findViewById(R.id.editText2);
    

    outside onCreate().That'll surely be an error as any view that has to be initialised has to be done in onCreate()

    So,Kindly move those two lines into your onCreate()