0

I am trying to transfer an int value between two activities using intents, but my app keeps crashing. When I comment out the transfer of any data and simply use an intent, everything seems to work. I cannot tell what is wrong.

Activity 1 (HeartRateActivity):

//Imports

public class HeartRateActivity extends Activity {
/** Called when the activity is first created. */
Button nextActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    nextActivity = (Button)findViewById(R.id.nextActivity);

    nextActivity.setOnClickListener(new Button.OnClickListener(){

        @Override
public void onClick(View v) {
    Intent intent = new Intent(HeartRateActivity.this, NextActivity.class);
    intent.putExtra("age", 2);
    startActivity(intent);
}

    });

}
}

My NextActivity.java

package com.heartRate;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;



public class NextActivity extends Activity {
TextView display;
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.next);
       int age = getIntent().getIntExtra("age", 0);
       display = (TextView) findViewById(R.id.display);
       display.setText(age);
}
}

My AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.heartRate" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".HeartRateActivity"
            android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" 
                             />
        </intent-filter>
    </activity>
    <activity android:name=".NextActivity" 
      android:label="@string/app_name" />
</application>
</manifest>

My main.xml (used by HeartRateActivity)

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
 >
 <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
 />
 <Button android:layout_width="wrap_content" android:layout_height="wrap_content"  
 android:id="@+id/nextActivity" android:text="nextActivity"></Button>
</LinearLayout>

My next.xml(used by NextActivity) is similar and i dont think thats the issue...:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
 <TextView android:text="TextView" android:layout_width="wrap_content" 
 android:layout_height="wrap_content" android:id="@+id/display"></TextView>

 </LinearLayout>

I would appreciate help in solving this issue! Thank you

Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30

5 Answers5

3

Replace

display.setText(age);

with

display.setText(Integer.toString(age));

If you provide an int as a parameter, it uses it as a resource ID, which, in this case, obviously doesn't exist.

Glendon Trullinger
  • 4,052
  • 1
  • 28
  • 35
1

just try do like this.

display = (TextView) findViewById(R.id.display);

display.setText(Integer.toString(age));

and surely it will work

jazz
  • 1,216
  • 7
  • 7
1

Make sure that your Id and Intent Destination all must be Ok.

then just put between intent from initialize ans start. with put

intent.putExtra("age", Double);

from get this.

double d = getIntent().getStringExtra("age");
Siten
  • 4,515
  • 9
  • 39
  • 64
0

Make sure that R.id.display is a valid TextView in your next layout.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • 06-18 03:46:21.749: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.heartRate/.HeartRateActivity } 06-18 03:46:21.779: DEBUG/AndroidRuntime(881): Shutting down VM 06-18 03:46:21.788: DEBUG/dalvikvm(881): DestroyJavaVM waiting for non-daemon threads to exit 06-18 03:46:21.788: ERROR/AndroidRuntime(881): ERROR: thread attach failed 06-18 03:46:21.818: DEBUG/dalvikvm(881): DestroyJavaVM shutting VM down – Kshitij Grover Jun 18 '11 at 03:47
  • 06-18 03:46:21.818: DEBUG/dalvikvm(881): HeapWorker thread shutting down 06-18 03:46:21.829: DEBUG/dalvikvm(881): HeapWorker thread has shut down 06-18 03:46:21.838: DEBUG/jdwp(881): JDWP shutting down net... 06-18 03:46:21.838: INFO/dalvikvm(881): Debugger has detached; object registry had 1 entries 06-18 03:46:21.848: DEBUG/dalvikvm(881): VM cleaning up 06-18 03:46:21.899: DEBUG/dalvikvm(881): LinearAlloc 0x0 used 639500 of 5242880 (12%) – Kshitij Grover Jun 18 '11 at 03:47
  • 06-18 03:46:26.988: ERROR/AndroidRuntime(865): Uncaught handler: thread main exiting due to uncaught exception 06-18 03:46:27.008: ERROR/AndroidRuntime(865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.heartRate/com.heartRate.NextActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x2 06-18 03:46:27.008: ERROR/AndroidRuntime(865): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 06-18 03:46:27.008: ERROR/AndroidRuntime(865): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) – Kshitij Grover Jun 18 '11 at 03:47
  • 06-18 03:46:27.008: ERROR/AndroidRuntime(865): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 06-18 03:46:27.008: ERROR/AndroidRuntime(865): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 06-18 03:46:27.008: ERROR/AndroidRuntime(865): at android.os.Handler.dispatchMessage(Handler.java:99) 06-18 03:46:27.008: ERROR/AndroidRuntime(865): at android.os.Looper.loop(Looper.java:123) 06-18 03:46:27.008: ERROR/AndroidRuntime(865): at android.app.ActivityThread.main(ActivityThread.java:4363) – Kshitij Grover Jun 18 '11 at 03:50
  • Glendon's answer looks like it. – kabuko Jun 18 '11 at 04:05
-1

Just so we're clear, you're saying when you include the ...

intent.putExtra("age", 2); 

in the sending Activity, and the ...

int age = getIntent().getIntExtra("age", 0);

in the receiving Activity, the application crashes or expereinces problems? And, by excluding those 2 statements the application performs normally? I'm just curious what does LogCat show? Even though you application is crashing LogCat will have several entries as to what was happening just prior to the close.

BonanzaDriver
  • 6,411
  • 5
  • 32
  • 35
  • yes, if I dont pass any information, it works fine. The app says it has closed unexpectedly and does not move on to the next screen. – Kshitij Grover Jun 18 '11 at 03:49