java.lang.NullPointerException

So I've been struggling with this for the past few hours and searching SO before I don't think I can find a way to sovle my problem. Basically I'm trying to use Context inside of the Fragment extension and it's giving me errors I've pasted the full code here in this gist

enter link description here

From what I found on SO it sounds like this happens when there is null for Context, but I don't understand how to fix it.

LogCat:

> 06-18 10:20:08.972    5718-5718/com.aparosecurity.kblock W/dalvikvm﹕
> threadid=1: thread exiting with uncaught exception (group=0x41625898)
> 06-18 10:20:08.992    5718-5718/com.aparosecurity.kblock
> E/AndroidRuntime﹕ FATAL EXCEPTION: main
>     java.lang.NullPointerException
>             at com.aparosecurity.kblock.ImageFragment.onCreate(ImageFragment.java:77)
>             at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
>             at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
>             at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:834)
>             at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:841)
>             at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
>             at android.support.v4.view.ViewPager.populate(ViewPager.java:1066)
>             at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
>             at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
>             at android.view.View.measure(View.java:15848)
>             at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
>             at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
>             at android.view.View.measure(View.java:15848)
>             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
>             at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
>             at android.view.View.measure(View.java:15848)
>             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
>             at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
>             at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
>             at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
>             at android.view.View.measure(View.java:15848)
>             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
>             at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
>             at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
>             at android.view.View.measure(View.java:15848)
>             at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
>             at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
>             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
>             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
>             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
>             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
>             at android.view.Choreographer.doCallbacks(Choreographer.java:562)
>             at android.view.Choreographer.doFrame(Choreographer.java:532)
>             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
>             at android.os.Handler.handleCallback(Handler.java:730)
>             at android.os.Handler.dispatchMessage(Handler.java:92)
>             at android.os.Looper.loop(Looper.java:137)
>             at android.app.ActivityThread.main(ActivityThread.java:5136)
>             at java.lang.reflect.Method.invokeNative(Native Method)
>             at java.lang.reflect.Method.invoke(Method.java:525)
>             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
>             at dalvik.system.NativeStart.main(Native Method)

Any help is appreciated.

The specific lines which are failing, the error occurs when mContext is used.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
    mHandler = new Handler();

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager =
            (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

}

You have not initialized mContext and it is null. Invoking a method on null reference causes the NPE.

In a fragment, use getActivity() to get a reference to the hosting activity that can be used as a Context . (Caveat: Only when attached to an activity. Works in onCreate() which comes after onAttach() .)

So, replace

mContext.getSystemService()

with

getActivity().getSystemService()
链接地址: http://www.djcxy.com/p/93044.html

上一篇: YouTubePlayerSupportFragment无法在播放器顶部添加视图

下一篇: 显示java.lang.NullPointerException