Android Intent机制实例详解(4)
package com.tope.samples.intent.simple;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SimpleIntentTest extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startBtn = (Button)findViewById(R.id.start_activity);
startBtn.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_activity:
Intent intent = new Intent(this, TestActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
上面的代码中,主要是为“Start activity”按钮添加了OnClickListener,使得按钮被点击时执行onClick()方法,onClick()方法中则利用了Intent机制,来启动TestActivity,关键的代码是下面这两行:
Intent intent = new Intent(this, TestActivity.class); startActivity(intent); |
这里定义Intent对象时所用到的是Intent的构造函数之一:
Intent(Context packageContext, Class<?> cls)
两个参数分别指定Context和Class,由于将Class设置为TestActivity.class,这样便显式的指定了TestActivity类作为该Intent的接收者,通过后面的startActivity()方法便可启动TestActivity。
TestActivity的代码更为简单(定义TestActivity类需要新建TestActivity.java文件,如果你是一个初学者,你需要学会如何在Eclipse或其他开发环境下添加一个新的类,本书不作详述,请参考其他文档),如下所示:
package com.tope.samples.intent.simple;
import android.app.Activity; import android.os.Bundle;
public class TestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_activity); } } |
可见TestActivity仅仅是调用setContentView来显示test_activity.xml中的内容而已。对于test_activity.xml及本例中所用到其他xml文件这里不作多余说明,读者练习时可自行参考本书所附光盘中的源代码。
如果我们仅仅是做上面的一些工作,还不能达到利用SimpleIntentTest启动TestActivity的目的。事实上,这样做会出现下面的Exception,导致程序退出。以下是利用logcat工具记录的log信息(省略了后半部分):
I/ActivityManager( 569): Displayed activity com.tope.samples/.SimpleIntentTest: 3018 ms I/ActivityManager( 569): Starting activity: Intent { comp={com.tope.samples/com.tope.samples.TestActivity} } D/AndroidRuntime( 932): Shutting down VM W/dalvikvm( 932): threadid=3: thread exiting with uncaught exception (group=0x4000fe70) E/AndroidRuntime( 932): Uncaught handler: thread main exiting due to uncaught exception E/AndroidRuntime( 932): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.tope.samples/com.tope.samples.TestActivity}; have you declared this activity in your AndroidManifest.xml? E/AndroidRuntime( 932): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1480) E/AndroidRuntime( 932): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1454) E/AndroidRuntime( 932): at android.app.Activity.startActivityForResult(Activity.java:2656) E/AndroidRuntime( 932): at android.app.Activity.startActivity(Activity.java:2700) E/AndroidRuntime( 932): at com.tope.samples.SimpleIntentTest.onClick(SimpleIntentTest.java:24) … |
posted on 2011-05-30 21:28 android爱好者 阅读(161) 评论(0) 编辑 收藏 举报