SSMA
SSMA

Reputation: 497

Start Activity of one application from another application in android

I am new to android development. My teacher has set us a task to develop an application and take text input in it. Then on button click send this data to another application and display it on that application. But when I click then button the application stops. This is my code so far

Layout xml

<EditText
    android:id="@+id/editText1"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignParentLeft="false"
    android:ems="10"
    android:inputType="textPersonName" />
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="180dp"
    android:text="Button 1"
    android:onClick="function1" />

function1 in mainActivity.java

EditText myEditText;
public void function1(View v){
    String userText=myEditText.getText().toString();
    Intent i= new Intent();
    i.setAction("textViewActivity");
    i.putExtra("message", userText);
    startActivity(i);
}

In the second application I have registered the activity as follows:

<activity
    android:name="Activity2"
    android:label="@string/a2" >
    <intent-filter>
        <action android:name="textViewActivity" />
    </intent-filter>
</activity>

in activity2.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout2);
    TextView mytextView=(TextView) findViewById(R.id.text2View1);
    String Value=getIntent().getExtras().getString("message");
    mytextView.setText(Value);
}

I have no idea where i did wrong. When I click button the program stops. Any help would be appreciated.

UPDATE: logcat:

02-11 22:43:52.449: D/gralloc_goldfish(1092): Emulator without GPU emulation detected.
02-11 22:43:56.759: D/AndroidRuntime(1092): Shutting down VM
02-11 22:43:56.759: W/dalvikvm(1092): threadid=1: thread exiting with uncaught exception (group=0xb3ab0b90)
02-11 22:43:56.829: E/AndroidRuntime(1092): FATAL EXCEPTION: main
02-11 22:43:56.829: E/AndroidRuntime(1092): Process: smd.homework1.question2, PID: 1092
02-11 22:43:56.829: E/AndroidRuntime(1092): java.lang.IllegalStateException: Could not execute method of the activity
02-11 22:43:56.829: E/AndroidRuntime(1092):     at android.view.View$1.onClick(View.java:3814)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at android.view.View.performClick(View.java:4424)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at android.view.View$PerformClick.run(View.java:18383)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at android.os.Handler.handleCallback(Handler.java:733)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at android.os.Looper.loop(Looper.java:137)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at android.app.ActivityThread.main(ActivityThread.java:4998)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at java.lang.reflect.Method.invokeNative(Native Method)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at java.lang.reflect.Method.invoke(Method.java:515)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at dalvik.system.NativeStart.main(Native Method)
02-11 22:43:56.829: E/AndroidRuntime(1092): Caused by: java.lang.reflect.InvocationTargetException
02-11 22:43:56.829: E/AndroidRuntime(1092):     at java.lang.reflect.Method.invokeNative(Native Method)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at java.lang.reflect.Method.invoke(Method.java:515)
02-11 22:43:56.829: E/AndroidRuntime(1092):     at android.view.View$1.onClick(View.java:3809)
02-11 22:43:56.829: E/AndroidRuntime(1092):     ... 11 more
02-11 22:43:56.829: E/AndroidRuntime(1092): Caused by: java.lang.NullPointerException
02-11 22:43:56.829: E/AndroidRuntime(1092):     at smd.homework1.question2.MainActivity.function1(MainActivity.java:26)
02-11 22:43:56.829: E/AndroidRuntime(1092):     ... 14 more

Upvotes: 0

Views: 174

Answers (3)

AI I
AI I

Reputation: 381

You're sending data from one ACTIVITY to another ACTIVITY! is that what you teacher asked? Or he asked to send data from one APPLICATION to another APPLICATION? if it's from one app to another app,you should implement Service and Broadcast Receiver. if it's from one activity to another activity within one application then @Sam already answered

Upvotes: 1

Sam
Sam

Reputation: 1662

In first application you should initialize your EditText in onCreate() after setContentView():

 myEditText=(EditText) findViewById(R.id.PUT_ID_HERE);

Also change this:

String userText=myEditText.getText().toString();

to:

String userText;
if(myEditText.getText()==null){
    userText = "";
}else{
    userText = myEditText.getText().toString();
}

Upvotes: 0

Michal
Michal

Reputation: 998

First you should specify your application package as prefix to all of your custom Intent actions, so instead of

<activity
    android:name="Activity2"
    android:label="@string/a2" >
    <intent-filter>
        <action android:name="textViewActivity" />
    </intent-filter>
</activity>

try

<activity
    android:name="Activity2"
    android:label="@string/a2" >
    <intent-filter>
        <action android:name="com.whatever.is.yourpackage.ACTION.textViewActivity" />
    </intent-filter>
</activity>

Also Android puts action category DEFAULT to all implicit intents passed to startActivity(). You should add this category to your intent-filter. I strongly suggest you to read this post http://developer.android.com/guide/components/intents-filters.html

Upvotes: 0

Related Questions