显示java.lang.NullPointerException

所以在过去的几个小时里,我一直在为此苦苦挣扎,并且在找不到解决我的问题的方法之前进行搜索。 基本上我试图在Fragment扩展中使用Context,它给了我错误,我在这个要点中粘贴了完整的代码

在此处输入链接描述

从我在SO上发现的情况来看,这听起来像是在Context有null时发生的,但我不明白如何解决这个问题。

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)

任何帮助表示赞赏。

特定行失败,使用mContext时发生错误。

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

}

你还没有初始化mContext ,它是空的。 在空引用上调用方法会导致NPE。

在片段中,使用getActivity()获取对可用作Context的托管活动的引用。 (警告:只有当附加到活动工程在onCreate()其配备后onAttach()

所以,更换

mContext.getSystemService()

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

上一篇: java.lang.NullPointerException

下一篇: Show view in front of soft keyboard