手记

No enclosing instance of type SignOutDialog is accessible.

最近在自定义dialog的时候,在编译写一个例子时。结果编译时出现:

No enclosing instance of type SignOutDialog is accessible. Must qualify the allocation with an enclosing instance of type SignOutDialog (e.g. x.new A() where x is an instance of SignOutDialog).

根据提示,没有可访问的内部类E的实例,必须分配一个合适的内部类E的实例

于是百度谷歌了一下相关资料。原来我写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态

方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。

所以在不做其他变动的情况下,最简单的解决办法是将public class改为public static class.

例子:

public static class Builder {//添加static即可!


private Context context;

private String message;

private String title;

private View contentView;

private String positiveButtonText;

private String negativeButtonText;

private DialogInterface.OnClickListener positiveButtonClickListener;

private DialogInterface.OnClickListener negativeButtonClickListener;


public Builder(Context context) {

this.context = context;


}


public Builder setMessage(String message) {//


this.message = message;

return this;

}

public Builder setMessage(int message){

this.message = (String) context.getText(message);

return this;

}


public Builder setTitle(String title) {

this.title = title;

return this;


}

public Builder setTitle(int title){

this.title = (String) context.getText(title);

return this;

}

public Builder setContentView(View v){

this.contentView = v;

return null;

}


public Builder setPositiveButton(String positiveButtonText,OnClickListener onClickListener) {

this.positiveButtonText = positiveButtonText;

this.positiveButtonClickListener = onClickListener;

return this;


}

public Builder setPositiveButton(int positiveButtonText,OnClickListener onClickListener) {

this.positiveButtonText = (String) context.getText(positiveButtonText);

this.positiveButtonClickListener = onClickListener;

return this;

}


public Builder setNegativeButton(String negativeButtonText,OnClickListener onClickListener) {

this.negativeButtonText = negativeButtonText;

this.negativeButtonClickListener = onClickListener;

return this;

}

public Builder setNegativeButton(int negativeButtonText,OnClickListener onClickListener) {

this.negativeButtonText = (String) context.getText(negativeButtonText);

this.negativeButtonClickListener = onClickListener;

return this;

}

原文链接:http://www.apkbus.com/blog-784586-61339.html

0人推荐
随时随地看视频
慕课网APP