开发者

Passing data between Intents causes app to 'unexpectedly close'

开发者 https://www.devze.com 2023-03-14 05:52 出处:网络
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

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 canno开发者_如何学编程t 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


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.


just try do like this.

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

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

and surely it will work


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");


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号