2016-12-30 97 views
-3

我是新手到Android,並在片段上的自定義列表視圖的工作,我已經用它我在那裏工作很好的活動,但是當我在片段用它它說「空指針」,任何人都可以請幫我弄明白嗎?獲得空指針異常列表適配器片段的Android

* ErroLog **

java.lang.NullPointerException 
    at abc.test.com.abc.adapter.ListingAdapter.getView(ListingAdapter.java:50) 
    at android.widget.AbsListView.obtainView(AbsListView.java:2758) 
    at android.widget.ListView.makeAndAddView(ListView.java:1811) 
    at android.widget.ListView.fillDown(ListView.java:697) 
    at android.widget.ListView.fillFromTop(ListView.java:763) 
    at android.widget.ListView.layoutChildren(ListView.java:1641) 
    at android.widget.AbsListView.onLayout(AbsListView.java:2579) 
    at android.view.View.layout(View.java:15630) 
    at android.view.ViewGroup.layout(ViewGroup.java:4884) 
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677) 
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531) 
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1440) 
    at android.view.View.layout(View.java:15630) 
    at android.view.ViewGroup.layout(ViewGroup.java:4884) 
    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1695) 
    at android.view.View.layout(View.java:15630) 
    at android.view.ViewGroup.layout(ViewGroup.java:4884) 
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
    at android.view.View.layout(View.java:15630) 
    at android.view.ViewGroup.layout(ViewGroup.java:4884) 
    at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:459) 
    at android.view.View.layout(View.java:15630) 
    at android.view.ViewGroup.layout(ViewGroup.java:4884) 
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
    at android.view.View.layout(View.java:15630) 
    at android.view.ViewGroup.layout(ViewGroup.java:4884) 
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2350) 
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2063) 
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251) 
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6557) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:613) 
    at android.view.Choreographer.doFrame(Choreographer.java:583) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5653) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
    at dalvik.system.NativeStart.main(Native Method) 

fragment.java

public class AllFragment extends Fragment { 
    ListingAdapter adapter; 
    String[] web = { 
      "Pavel City Fire Brigade", 
      "Lonavala fire Station", 
      "Amravati Fire Base", 
      "Chakan City Fire Brigade", 
      "Talegaon Fire Base", 
      "Thane Fire brigade", 
      "Khar Fire Base", 
      "Virar Fire Station", 
      "Bhakala Fire Base", 
    }; 
    int[] imageId = { 
      R.drawable.img_emergency, 
      R.drawable.img_emergency, 
      R.drawable.img_emergency, 
      R.drawable.img_emergency, 
      R.drawable.img_emergency, 
      R.drawable.img_emergency, 
      R.drawable.img_emergency, 
      R.drawable.img_emergency, 
      R.drawable.img_emergency, 
    }; 
    Context context = getActivity(); 
    ListView lv_listing; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_all, container, false); 
     lv_listing = (ListView)rootView.findViewById(R.id.lv_listing); 

     adapter = new ListingAdapter(context, web, imageId); 
     lv_listing.setAdapter(adapter); 

     return rootView; 
    } 
} 

listAdapter.java

public class ListingAdapter extends BaseAdapter { 
    private Context mContext; 
    private final String[] web; 

    public ListingAdapter(Context c, String[] web, int[] Imageid) { 
     mContext = c; 
     this.web = web; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return web.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View grid; 
     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 

      grid = new View(mContext); 
      grid = inflater.inflate(R.layout.raw_tab, null); 
      TextView tv_cat = (TextView) grid.findViewById(R.id.tv_cat); 
      ImageView iv_pic = (ImageView)grid.findViewById(R.id.iv_pic); 
      TextView tv_features = (TextView)grid.findViewById(R.id.tv_features); 
      TextView tv_location = (TextView)grid.findViewById(R.id.tv_location); 
      TextView tv_rating = (TextView)grid.findViewById(R.id.tv_rating); 
      RatingBar rt_bar = (RatingBar)grid.findViewById(R.id.rt_fvrt); 
      TextView tv_votes = (TextView)grid.findViewById(R.id.tv_votes); 
      ImageView iv_call = (ImageView)grid.findViewById(R.id.iv_call); 
      ImageView iv_share = (ImageView)grid.findViewById(R.id.iv_share); 

      tv_cat.setText(web[position]); 
     } else { 
      grid = (View) convertView; 
     } 

     return grid; 
    } 
} 

raw_tab

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg_category" 
    android:padding="5dp"> 


    <ImageView 
     android:id="@+id/iv_pic" 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:layout_alignParentLeft="true" 
     android:layout_margin="10dp" 
     android:scaleType="fitXY" 
     android:src="@drawable/img_entertainment" /> 

    <TextView 
     android:id="@+id/tv_cat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:layout_toLeftOf="@+id/iv_call" 
     android:layout_toRightOf="@+id/iv_pic" 
     android:text="Super mall,Panvel" 
     android:textColor="@color/black" 
     android:textSize="20dp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/tv_features" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_cat" 
     android:layout_marginTop="2dp" 
     android:layout_toRightOf="@+id/iv_pic" 
     android:maxLines="2" 
     android:text="Featurs:ICU,Heart Care,Cardiac,Emergency,Orthopedic" 
     android:textColor="@color/grey_dark" 
     android:textSize="13dp" /> 

    <ImageView 
     android:id="@+id/iv_loca" 
     android:layout_width="15dp" 
     android:layout_height="15dp" 
     android:layout_below="@+id/tv_features" 
     android:layout_marginLeft="-2dp" 
     android:layout_marginTop="2dp" 
     android:layout_toRightOf="@+id/iv_pic" 
     android:background="@drawable/ic_location" /> 

    <TextView 
     android:id="@+id/tv_location" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_features" 
     android:layout_marginTop="2dp" 
     android:layout_toRightOf="@+id/iv_loca" 
     android:maxLines="2" 
     android:text="Navi Mumbai" 
     android:textColor="@color/grey_dark" 
     android:textSize="13dp" /> 

    <RelativeLayout 
     android:id="@+id/rl_bottom" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_location" 
     android:layout_marginLeft="-10dp" 
     android:layout_marginTop="5dp" 
     android:layout_toRightOf="@+id/iv_loca"> 

     <TextView 
      android:id="@+id/tv_rating" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/bg_green" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:text="3.0" 
      android:textColor="#fff" 
      android:textSize="14dp" /> 

     <RatingBar 
      android:id="@+id/rt_fvrt" 
      style="@android:style/Widget.Holo.Light.RatingBar.Small" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_toRightOf="@+id/tv_rating" 
      android:numStars="5" /> 

     <TextView 
      android:id="@+id/tv_votes" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_toRightOf="@+id/rt_fvrt" 
      android:text="2 Votes" 
      android:textColor="@color/grey_dark" 
      android:textSize="14dp" /> 

    </RelativeLayout> 

    <ImageView 
     android:id="@+id/iv_call" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/ic_call_green" /> 

    <ImageView 
     android:id="@+id/iv_share" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/iv_call" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="20dp" 
     android:background="@drawable/bg_et" 
     android:padding="5dp" 
     android:src="@drawable/ic_shr" /> 
</RelativeLayout> 

fragment_all.xml

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

    <ListView 
     android:id="@+id/lv_listing" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"></ListView> 

</LinearLayout> 

請幫助我。

+0

請分享你的錯誤日誌 – Shubh

+0

@Shubh - 增加..Please檢查 –

+0

@JigarMakwana可能還發布您的活動? –

回答

2

在你AllFragment變化

adapter = new ListingAdapter(context, web, imageId); 

到:

adapter = new ListingAdapter(getActivity(), web, imageId); 

,並在您ListingAdapter變化

public ListingAdapter(context c, String[] web, int[] Imageid) { 
     mContext = c; 
     this.web = web; 
    } 

public ListingAdapter(Activity c, String[] web, int[] Imageid) { 
     mContext = c; 
     this.web = web; 
    } 

,改變

private Context mContext; 

private Activity mContext; 

,改變

LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

到:

LayoutInflater inflater = (LayoutInflater) mContext.getLayoutInflater();