简体   繁体   English

如何从在android中扩展Activity的类的函数获取上下文?

[英]How to get the context from a function of a class that extends Activity in android?

I have a class that extends Activity, When I try to access the context from the onCreate() method it gets printed but when I save it in a variable context and try to access it from the committext() function as shown below, It prints null.我有一个扩展 Activity 的类,当我尝试从 onCreate() 方法访问上下文时,它会被打印出来,但是当我将它保存在一个变量上下文中并尝试从committext()函数访问它时,如下所示,它会打印空值。 I also tried using "this" and getBaseContext() directly from the committext() function as it is a part of the same class, I get null pointer exception.我还尝试直接从committext()函数中使用“this”和getBaseContext() ,因为它是同一类的一部分,我得到空指针异常。 Please help me figure out what is going wrong.请帮我弄清楚出了什么问题。

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();    
    }
}

使您的myHandler类不是static

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:如何从扩展应用程序的 class 将应用程序上下文作为活动获取? - Android: How do I get the Application Context as an Activity from a class that extends Application? 在类中获取活动上下文,该类在Android中扩展了Application - Get Activity Context within class which extends Application in android Android-如何从Java类传递另一个活动应用程序上下文将片段扩展到asynctask? - Android - How to pass another activity application context from java class extends fragments to asynctask? 如何从Android 4.3中的类(此类扩展LinearLayout)启动Activity? - How to start Activity from a class( this class extends LinearLayout ) in Android 4.3? Android从非活动类获取上下文 - Android get context from non Activity Class 如何从不扩展Activity,Android的类中创建DatePicker? - How to create a DatePicker from class which do not extends Activity, Android? 如何从不扩展 Activity、Android 的类创建 TimePicker? - How to create a TimePicker from class which do not extends Activity, Android? 如何获取扩展片段/活动等的 class 的 class 的视图 - How to get the View for a class, that extends a class that extends Fragment/activity etc 如何将活动上下文放入非活动类android? - How to get activity context into a non-activity class android? 如何扩展两类Android活动? - how to extends two class Android activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM