当我打电话
时,我的问题就出现了 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);
}
}
答案 0 :(得分:1)
您是否添加了您在清单文件中启动的活动?
如果您从Handler
开始,这一点并不重要,因为您提供了使用.startActivity()
的正确上下文。我想你错过了在清单中添加活动。