2011-12-12 63 views
0

我想找到一个切换按钮在非活动课,但我得到一个NullPointerException异常。我也很喜欢下面的代码:我如何在非活动类中找到视图?

public class ClassMode{ 
    public static boolean isClassMode(Context c) { 
    ToggleButton t = new ToggleButton(c); 
    t = (ToggleButton) t.findViewById(R.id.classmode); 
    if (t.isChecked()) { 
     return true; 
    } else { 
     return false; 
    } 
} 

public static void setNextMode(final Context context){ 
    if(isClassMode){ 
     dosomething(); 
    } 
    } 
} 

我都做什么了?我是一个新的progromer,我想这个问题可能什么也不是。但对我来说却是相反的。 有人帮助我,请期待您的回复。 3Q!

+0

请将th e LogCat提取,显示异常 – Guillaume

回答

0

findViewById将在您调用它的对象内找到一个视图。通常它被称为一项活动,但也可以在有孩子的任何ViewGroup上调用。

在你的情况下,你可以在Button对象上调用它。我怀疑这个按钮包含一个id为R.id.classmode的孩子。

尝试:

c.findViewById(R.id.classmode) 

(注:你应该张贴与你得到的实际异常的logcat的,否则我们只是猜测)

+0

非常感谢。我可能知道为什么。 – IdroidCheung

0

更改的行

t = (ToggleButton) t.findViewById(R.id.classmode); 

t = (ToggleButton) findViewById(R.id.classmode); 
相关问题