2013-11-21 177 views
0

爲什麼當我指向那裏的東西時,我得到一個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

請張貼日誌貓輸出。 – salrawaf

+0

添加了我的Logcat。這是有點長 - –

回答

4

試試這個..

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

,而不是這個..

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

因爲V父視圖,所以你需要使用這個..

+0

非常感謝你!這解決了我所有的努力。我的頭髮在這上面掉了一會兒! –

+0

@ D-Rock歡迎我接受我的答覆。 – Hariharan

+0

完成!我正在等待櫃檯下山。我在信貸到期時給予信用=] –

相關問題