——排错:
Activity02:
package com.example.activity02;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity02 extends Activity {
private Button myButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02_layout);
//Button myButton=(Button)findViewById(R.id.myButton);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new myButtonListener());
}
class myButtonListener implements OnClickListener{
public void onClick(View v){
//生成一个intent对象
Intent intent = new Intent();
intent.putExtra("testIntent", "123");
//从Activity02的intent,跳转到otherActivity
intent.setClass(Activity02.this, otherActivity.class);
Activity02.this.startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity02, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
otherActivity:
package com.example.activity02;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class otherActivity extends Activity {
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String value = intent.getStringExtra("testIntent");
myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(value);
}
}
activity01_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.activity02.Activity02" >
<TextView
android:id="@+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
other.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.activity02.Activity02" >
<TextView
android:id="@+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Activity02</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="other">otherActivity</string>
</resources>
R.java
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Activity02</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="other">otherActivity</string>
</resources>
Activity01Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity02"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity02"
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=".otherActivity" android:label="@string/other"/>
</application>
</manifest>
输出结果:
错误原因:
otherActivity:
package com.example.activity02;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class otherActivity extends Activity {
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//忘记设置otherActivity使用layout的布局了(string的设置)
setContentView(R.layout.other);
Intent intent = getIntent();
String value = intent.getStringExtra("testIntent");
myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(value);
}
}