public class SlideAdapter extends PagerAdapter {
private Intent hauptmenu;
hauptmenu = new Intent (this, com.stw.myapplication.Hauptmenu.class);
答案 0 :(得分:0)
不是您不能以这种方式声明tableB
,而是您不能以这种方式声明任何内容。
在Java中,您可以通过以下方式声明实例:
is_initialized
或
Intent
这是您熟悉的常规方法。
但是,您无法以所需的方式进行声明和初始化:
private Intent hauptmenu; //Needs to be initialized later
这是因为//Declares and initializes
private Intent hauptmenu = new Intent(this, com.stw.myapplication.Hauptmenu.class);
是 Statement (声明),并且普通语句不能在Statement Block(例如方法块)之外运行。
括号用大括号{}表示。
因此,如果要在构造函数或方法之外初始化对象,则必须将代码放置在称为“初始化程序块”的块中,如下所示:
private Intent hauptmenu;
hauptmenu = new Intent (this, com.stw.myapplication.Hauptmenu.class);
使用Initializer Blocks,您甚至可以运行其他类型的代码(以记录我创建Intent的示例显示)。在声明过程中初始化变量时,这是您无法做到的,除了故意创建方法之外,。
因此,要回答您的问题,您不能以您想要的方式声明hauptmenu = new Intent (this, com.stw.myapplication.Hauptmenu.class);
,因为Java不允许在声明之外的除声明语句之外的语句一个街区。
因此,如果您想以自己想要的方式创建Intent,则只需在语句周围添加花括号。这将使您的初始化语句位于Initializer Block内,从而使其可以运行。
答案 1 :(得分:0)
您使用的Intent构造函数需要第一个参数的上下文。但是,由于您在扩展PagerAdapter的类中,因此“ this”关键字不会引用上下文。