就是这个帖子
里面给了个方法:
在a里面设置一个静态的变量instance,初始化为this
在D里面,a.instance.finish()
请问这个instance应该怎么设置?
我用
public class a extends Activity
{
static a instance=a.this;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
提示我"不能在静态上下文中使用this"
11 个解决方案
#1
public class a extends Activity
{
public static a instance=a.this;
}
public class B extends Activity
{
public void closeA() {
a.instance.finish();
}
}
{
public static a instance=a.this;
}
public class B extends Activity
{
public void closeA() {
a.instance.finish();
}
}
#2
静态数据成员属于类的,所以直接用就可以。
a.instance.finish();
a.instance.finish();
#3
public class a extends Activity
{
public static a instance=a.this;
}
public class B extends Activity
{
public void closeA() {
a.instance.finish();
}
}
直接关……
{
public static a instance=a.this;
}
public class B extends Activity
{
public void closeA() {
a.instance.finish();
}
}
直接关……
#4
楼上正解,直接关就行了。
#5
1楼的不行,还是跟我说不能在静态上下文中使用 this
#6
楼上几位好好看问题啊...我的问题不是关不掉,而是定义instance时就报错
#7
a instance=a.this;
这句要移到a的oncreate事件中执行
这句要移到a的oncreate事件中执行
#8
public class A_activity extends Activity {
public static A_activity _instance = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
_instance = this;
}
}
public class B_activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
A_activity._instance.finish();
}
}
静态变量不能这么赋值
public static A_activity _instance = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
_instance = this;
}
}
public class B_activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
A_activity._instance.finish();
}
}
静态变量不能这么赋值
#9
在public class XXX extends Activity
/** Called when the activity is first created. */
这里定义
public static XXXX xx;
其他地方用XXXX.xx.finish()
/** Called when the activity is first created. */
这里定义
public static XXXX xx;
其他地方用XXXX.xx.finish()
#10
顶八楼的方法,已试,可行。
#11
8楼方法经测试可行
#1
public class a extends Activity
{
public static a instance=a.this;
}
public class B extends Activity
{
public void closeA() {
a.instance.finish();
}
}
{
public static a instance=a.this;
}
public class B extends Activity
{
public void closeA() {
a.instance.finish();
}
}
#2
静态数据成员属于类的,所以直接用就可以。
a.instance.finish();
a.instance.finish();
#3
public class a extends Activity
{
public static a instance=a.this;
}
public class B extends Activity
{
public void closeA() {
a.instance.finish();
}
}
直接关……
{
public static a instance=a.this;
}
public class B extends Activity
{
public void closeA() {
a.instance.finish();
}
}
直接关……
#4
楼上正解,直接关就行了。
#5
1楼的不行,还是跟我说不能在静态上下文中使用 this
#6
楼上几位好好看问题啊...我的问题不是关不掉,而是定义instance时就报错
#7
a instance=a.this;
这句要移到a的oncreate事件中执行
这句要移到a的oncreate事件中执行
#8
public class A_activity extends Activity {
public static A_activity _instance = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
_instance = this;
}
}
public class B_activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
A_activity._instance.finish();
}
}
静态变量不能这么赋值
public static A_activity _instance = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
_instance = this;
}
}
public class B_activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
A_activity._instance.finish();
}
}
静态变量不能这么赋值
#9
在public class XXX extends Activity
/** Called when the activity is first created. */
这里定义
public static XXXX xx;
其他地方用XXXX.xx.finish()
/** Called when the activity is first created. */
这里定义
public static XXXX xx;
其他地方用XXXX.xx.finish()
#10
顶八楼的方法,已试,可行。
#11
8楼方法经测试可行