如果处理程序调用其中一个活动,如何在 Android 中的活动之间切换
当我调用它时,我的问题就出现了,
Intent i = new Intent(c.getApplicationContext(),ActivityMulti.class);
c.startActivity(i);
它会生成下面提到的异常:
10-02 17:54:26.037: ERROR/AndroidRuntime(905): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{package.com/package.ActivityMulti}:
java.lang.InstantiationException: package.ActivityMulti
这在处理程序中被调用,生成如下:
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
msg.setData(b);
handler.sendMessage(msg);
第二个活动是一个简单的活动:
public class ActivityMulti extends Activity{
public ActivityMulti(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
init();
}
private void init(){
TextView tv = new TextView(this);
tv.setText("This is activity multi");
setContentView(tv);
//ImageButton info = (ImageButton)findViewById(R.id.info);
//info.setImageResource(R.drawable.info2);
//this.setContentView(R.layout.view_multi);
}
}
为什么会生成该异常?你能帮助我吗?
编辑: 这是我的清单:
.
.
.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityMulti"></activity>
</application>
</manifest>
编辑 2:
我解决了!问题出在第二个 Activity 的构造函数中。没有必要!
public class ActivitySingle extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init(){
TextView tv = new TextView(this);
tv.setText("This is activity single");
setContentView(tv);
//ImageButton info = (ImageButton)findViewById(R.id.info);
//info.setImageResource(R.drawable.info2);
//this.setContentView(R.layout.view_multi);
}
}
My problem borns when i call
Intent i = new Intent(c.getApplicationContext(),ActivityMulti.class);
c.startActivity(i);
It generates the exception mentioned below:
10-02 17:54:26.037: ERROR/AndroidRuntime(905): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{package.com/package.ActivityMulti}:
java.lang.InstantiationException: package.ActivityMulti
This is called in an handler generate like this:
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
msg.setData(b);
handler.sendMessage(msg);
The second Activity is a simple one:
public class ActivityMulti extends Activity{
public ActivityMulti(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
init();
}
private void init(){
TextView tv = new TextView(this);
tv.setText("This is activity multi");
setContentView(tv);
//ImageButton info = (ImageButton)findViewById(R.id.info);
//info.setImageResource(R.drawable.info2);
//this.setContentView(R.layout.view_multi);
}
}
Why that exception is generated? Can you help me?
EDIT:
This is my manifest:
.
.
.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityMulti"></activity>
</application>
</manifest>
EDIT 2:
I Solved! the problem was in constructor of the second Activity.. There is no need of it!
public class ActivitySingle extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init(){
TextView tv = new TextView(this);
tv.setText("This is activity single");
setContentView(tv);
//ImageButton info = (ImageButton)findViewById(R.id.info);
//info.setImageResource(R.drawable.info2);
//this.setContentView(R.layout.view_multi);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否已在清单文件中添加了要启动的活动?
如果您从
Handler
启动它并不重要,因为您已经为使用.startActivity()
提供了正确的上下文。我认为您错过了在清单中添加活动。Have you added the activity you are starting in the Manifest file?
It's not important if you are starting it from
Handler
, since you have provided the right context for using.startActivity()
. I think you have missed to add the activity in the manifest.