Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing data between Intents causes app to 'unexpectedly close'
    text
    copied!<p>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.</p> <p>Activity 1 (HeartRateActivity):</p> <pre><code>//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); } }); } } </code></pre> <p>My NextActivity.java</p> <pre><code>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); } } </code></pre> <p>My AndroidManifest.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.heartRate" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-sdk android:minSdkVersion="7" /&gt; &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt; &lt;activity android:name=".HeartRateActivity" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".NextActivity" android:label="@string/app_name" /&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>My main.xml (used by HeartRateActivity)</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /&gt; &lt;Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/nextActivity" android:text="nextActivity"&gt;&lt;/Button&gt; &lt;/LinearLayout&gt; </code></pre> <p>My next.xml(used by NextActivity) is similar and i dont think thats the issue...:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/display"&gt;&lt;/TextView&gt; &lt;/LinearLayout&gt; </code></pre> <p>I would appreciate help in solving this issue! Thank you</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload