80,351
社区成员
发帖
与我相关
我的任务
分享
public class MainActivity extends Activity implements OnClickListener {
Button submit;
TextView view1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button) this.findViewById(R.id.submit);
view1 = (TextView) this.findViewById(R.id.view1);
submit.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SubActivity.class);// 从哪里来,到哪里去
intent.putExtra("view1", view1.getText());
intent.putExtra("view11", view1.getText() + "1123");
startActivity(intent);//注释掉这里,不会报错。所以应该是这里出错了
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.t1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".SubActivity"
android:label="@string/app_name" />
</application>
</manifest>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name: " />
<TextView
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="张三丰" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp" >
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="提交" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="用户名: " />
<TextView
android:id="@+id/subview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="62dp"
android:layout_weight="1"
android:text="" />
</LinearLayout>
package com.example.t1;
import android.app.Activity;
import android.os.Bundle;
public class SubActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.sub);
TextView subview1 = (TextView) this.findViewById(R.id.subview1);
Intent intent = this.getIntent(); // 获得当前的Intent
String value = intent.getStringExtra("view1"); // 获得名为name的值
String value1 = intent.getStringExtra("view11"); // 获得名为name的值
subview1.setText(value + "=" + value1);
}
}
intent.putExtra("view1", view1.getText().toString());
intent.putExtra("view11", view1.getText().toString() + "1123");
MainActivity.this.startActivity(intent);