我正在尝试使用意图在两个活动之间传输一个 int 值,但我的应用程序不断崩溃。当我注释掉任何数据的传输并简单地使用意图时,一切似乎都正常。我不能说什么是错的。
活动 1(心率活动):
//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);
}
});
}
}
我的 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);
}
}
我的 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>
我的 main.xml(由 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>
我的 next.xml(由 NextActivity 使用)是相似的,我认为这不是问题......:
<?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>
我将不胜感激帮助解决这个问题!谢谢