同一个.java文件中为什么不能定义两个activity? 15
一个Activity不是以一个类的形式写在一个.java文件中嘛,而且类名要和.java文件名相同,例如,我的一个activity定义:publicclassFirstA...
一个Activity不是以一个类的形式写在一个.java文件中嘛,而且类名要和.java文件名相同,例如,我的一个activity定义:public class FirstAppActivity extends Activity {}
然后它保存在FirstAppActivity.java文件中,在这个FirstAppActivity.java文件中为什么我定义另外一个activity类,例如SecondAppActivity extends Activity{}后(已取消掉SecondAppActivity的public标识)可以通过编译,只不过在first中调用second时程序就崩溃了
我查到了,由于activity必须是public的,而Java不允许在一个.java文件中存在两个public类,所以一个.java文件中只能包含一个activity
Q:I am new to android development and am currently working my way through the "Hello..." Tutorials on the developer website.
I got stuck on the Tab Layout walkthrough and the only way I could resolve it was to put each Activity Class in a separate .java file.
I was wondering if all Activities need to be in separate .java files, or am I missing something.
A:There can only be one public class in a Java file and it must have the same name as the file.
Why are you trying to get them into one file to begin with?
Q:Right, I am with you now. I just wanted one file which would have all the classes in it, I didn't realise that Activity Classes have to be public. Thanks 展开
然后它保存在FirstAppActivity.java文件中,在这个FirstAppActivity.java文件中为什么我定义另外一个activity类,例如SecondAppActivity extends Activity{}后(已取消掉SecondAppActivity的public标识)可以通过编译,只不过在first中调用second时程序就崩溃了
我查到了,由于activity必须是public的,而Java不允许在一个.java文件中存在两个public类,所以一个.java文件中只能包含一个activity
Q:I am new to android development and am currently working my way through the "Hello..." Tutorials on the developer website.
I got stuck on the Tab Layout walkthrough and the only way I could resolve it was to put each Activity Class in a separate .java file.
I was wondering if all Activities need to be in separate .java files, or am I missing something.
A:There can only be one public class in a Java file and it must have the same name as the file.
Why are you trying to get them into one file to begin with?
Q:Right, I am with you now. I just wanted one file which would have all the classes in it, I didn't realise that Activity Classes have to be public. Thanks 展开
4个回答
展开全部
SecondAppActivity是内部类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
重复可以的,可以参考android SDK里的sample例子, 建议你check一下manifest.xml里是否添加了你的MyActivity
更多追问追答
追问
已经添加了
追答
还是那个错误吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
麻烦楼主把代码贴上来,你的定义方式是正确的,应该是代码有问题!
更多追问追答
追问
很简单的,我在FirstAppActivity中定义了一个basic_button响应函数,启动同一个.java文件下定义的MyActivity,结果一点这个button就崩溃
basic_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(FirstAppActivity.this,MyActivity.class));
}
});
追答
是不是贴不下啊?不贴代码怎么解决问题啊?程序崩溃时出现什么异常还是出现error?描述的太笼统了!
-----------------------------------------------------------------------------
请问楼主,程序崩溃时什么意思?能不能把异常消息贴出来看看!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询