为什么我得到NullPointerException?

时间:2013-11-21 01:00:44

标签: java android nullpointerexception

为什么当我指向那里的东西时,我得到NullPointerException?

View header = (View)inflater.inflate(R.layout.listview_header_row, null);

类代码

public class FeaturedDealsFragment extends Fragment {

@Override  
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  

    View V = inflater.inflate(R.layout.fragment_featured_deals, container, false);
    ListView LV = (ListView) this.getActivity().findViewById(R.id.dealsListView);

    Deal deal_data[] = new Deal[]
            {
                new Deal("Deal 1", R.drawable.scenery, "500/100 Supporters", "$500"),
                new Deal("Deal 2", R.drawable.scenery, "500/100 Supporters", "$500"),
                new Deal("Deal 3", R.drawable.scenery, "500/100 Supporters", "$500"),
                new Deal("Deal 4", R.drawable.scenery, "500/100 Supporters", "$500"),
                new Deal("Deal 5", R.drawable.scenery, "500/100 Supporters", "$500")
            };

    DealAdapter adapter = new DealAdapter(getActivity(), R.layout.listview_item_row, deal_data);

    View header = (View)inflater.inflate(R.layout.listview_header_row, null);

    LV.addHeaderView(header);
    LV.setAdapter(adapter);

    return V;

}

}

listview_header_row XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"> 

<TextView android:id="@+id/txtHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#ffffff"
    style="@style/fragment_main_heading"
    android:textColor="#000000"
    android:text="@string/featuredDeals" />

</LinearLayout>

logcat的

11-20 16:53:10.868: E/AndroidRuntime(29998): FATAL EXCEPTION: main
11-20 16:53:10.868: E/AndroidRuntime(29998): java.lang.NullPointerException
11-20 16:53:10.868: E/AndroidRuntime(29998):    at com.karkinos.croupon.FeaturedDealsFragment.onCreateView(FeaturedDealsFragment.java:31)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.View.measure(View.java:15395)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.View.measure(View.java:15395)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.View.measure(View.java:15395)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2362)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.View.measure(View.java:15395)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1985)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1226)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1399)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4553)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.Choreographer.doFrame(Choreographer.java:525)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.os.Handler.handleCallback(Handler.java:615)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.os.Looper.loop(Looper.java:137)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at android.app.ActivityThread.main(ActivityThread.java:4950)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at java.lang.reflect.Method.invokeNative(Native Method)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at java.lang.reflect.Method.invoke(Method.java:511)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
11-20 16:53:10.868: E/AndroidRuntime(29998):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:4)

试试这个..

ListView LV = (ListView) V.findViewById(R.id.dealsListView);

而不是这个..

ListView LV = (ListView) this.getActivity().findViewById(R.id.dealsListView);

因为V父视图所以你需要使用它..