在动态创建的活动中使用共享首选项

时间:2016-07-04 01:56:03

标签: android sharedpreferences

我有一个没有onCreate()方法的动态创建的Activity。我在其中一个onTouch()方法中使用SharedPreferences时遇到了麻烦,该方法在我的createView()方法中。当我调用getSharedPreferences()时,我得到一个空指针异常。我猜测上下文为null,因为getApplicationContext()和getBaseContext()都会导致空指针异常。在这种情况下如何使用共享首选项?

这是我的createView()代码:

public void createView(){
    params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);

    RelativeLayout mainView = new RelativeLayout(service);

    LinearLayout l = new LinearLayout(service);
    RelativeLayout.LayoutParams param_one = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    l.setTag(first_tag);
    l.setOrientation(LinearLayout.VERTICAL);

    EditText second_test = new EditText(service);
    second_test.setTextColor(Color.LTGRAY);
    second_test.setWidth(440);
    second_test.setTag(second_tag);
    second_test.setTextSize(20);
    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    second_test.setLayoutParams(llp);
    l.addView(second_test);
    mainView.addView(l, param_one);
    LinearLayout bottomLayout = new LinearLayout(service);
    bottomLayout.setBackgroundColor(0x00ff0000);
    RelativeLayout.LayoutParams bottomparam = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

    bottomLayout.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d(TAG, "onTouch");
            View k = (View)v.getParent();
            LinearLayout l = (LinearLayout) k.findViewWithTag(first_tag);
            EditText second_test = (EditText) l.findViewWithTag(second_tag);

            if(!second_test.getText().toString().isEmpty()){
                String result = second_test.getText().toString();
                Log.d(TAG, "Result is " + result);
                SharedPreferences prefs = getSharedPreferences(TAG, MODE_PRIVATE);
                SharedPreferences.Editor editor = prefs.edit();
                editor.putString("result", result);
                editor.commit();
                finish();
            }
            return true;
        }
    });
    mainView.addView(bottomLayout, bottomparam);
    overlayView = mainView;     
}

这是logcat输出:

07-04 09:52:47.295: D/OverlayActivity(6146): onTouch
07-04 09:52:47.295: D/OverlayActivity(6146): Result is u=qwe&p=asd
07-04 09:52:47.295: E/InputEventReceiver(6146): Exception dispatching input event.
07-04 09:52:47.295: E/MessageQueue-JNI(6146): Exception in MessageQueue callback: handleReceiveCallback
07-04 09:52:47.305: E/MessageQueue-JNI(6146): java.lang.NullPointerException
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:161)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at com.example.test.OverlayActivity$2.onTouch(OverlayActivity.java:186)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.View.dispatchTouchEvent(View.java:7293)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2179)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1914)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.View.dispatchPointerEvent(View.java:7514)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3679)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3623)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4798)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4777)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4873)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:208)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.os.MessageQueue.nativePollOnce(Native Method)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.os.MessageQueue.next(MessageQueue.java:125)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.os.Looper.loop(Looper.java:135)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at android.app.ActivityThread.main(ActivityThread.java:5751)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at java.lang.reflect.Method.invokeNative(Native Method)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at java.lang.reflect.Method.invoke(Method.java:511)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
07-04 09:52:47.305: E/MessageQueue-JNI(6146):   at dalvik.system.NativeStart.main(Native Method)
07-04 09:52:47.305: W/dalvikvm(6146): threadid=1: thread exiting with uncaught exception (group=0x40c34a08)
07-04 09:52:47.310: E/AndroidRuntime(6146): FATAL EXCEPTION: main
07-04 09:52:47.310: E/AndroidRuntime(6146): java.lang.NullPointerException
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:161)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at com.example.test.OverlayActivity$2.onTouch(OverlayActivity.java:186)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.View.dispatchTouchEvent(View.java:7293)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2179)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1914)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1886)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.View.dispatchPointerEvent(View.java:7514)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3679)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3623)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4798)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4777)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4873)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:208)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.os.MessageQueue.nativePollOnce(Native Method)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.os.MessageQueue.next(MessageQueue.java:125)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.os.Looper.loop(Looper.java:135)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at android.app.ActivityThread.main(ActivityThread.java:5751)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at java.lang.reflect.Method.invoke(Method.java:511)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
07-04 09:52:47.310: E/AndroidRuntime(6146):     at dalvik.system.NativeStart.main(Native Method)

我做错了什么?

1 个答案:

答案 0 :(得分:0)

好的,事实证明我可以在构造函数中使用上下文。傻我。