我有一个扩展Activity的类,当我尝试从onCreate()方法访问上下文时,它会被打印但是当我将它保存在变量上下文中并尝试从committext()
函数访问它时如下所示,它打印为null。我也试过用#34;这个"和getBaseContext()
直接来自committext()
函数,因为它是同一个类的一部分,我得到空指针异常。请帮我弄清楚出了什么问题。
public class MainKeyboardActionListener extends Activity implements KeyboardView.OnKeyboardActionListener, View.OnTouchListener {
private Context context;
public static boolean active = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mode_keyboard, false);
context=getBaseContext();
System.out.println("contexxtt1"+context);
}
@Override
public void onStart() {
super.onStart();
active = true;
}
@Override
protected void onResume() {
super.onResume();
}
private static class myHandler extends Handler {
}
;
public void setInputConnection(InputConnection ic) {
}
@Override
public void onKey(int arg0, int[] arg1) {
// TODO Auto-generated method stub
}
@Override
public void onPress(int keyCode) {
}
@Override
public void onRelease(int keyCode) {
commitText(String.valueOf(keyCode));
}
private void handleException(int keyCode) {
}
private void removeHalantMode() {
}
private void commitText(String text) {
if(active==true) {
System.out.println("contexxtt"+context);
}
}
@Override
public void onText(CharSequence arg0) {
// TODO Auto-generated method stub
}
@Override
public void swipeDown() {
// TODO Auto-generated method stub
}
@Override
public void swipeLeft() {
// TODO Auto-generated method stub
}
@Override
public void swipeRight() {
// TODO Auto-generated method stub
}
@Override
public void swipeUp() {
// TODO Auto-generated method stub
}
@Override
protected void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
active = false;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
答案 0 :(得分:0)
让myHandler
课程不是static
。