2015-02-08 53 views
1

我有一個gridview來顯示來自url的圖像。我想用this library添加拉到刷新功能。但是,我得到錯誤,我不知道什麼logcat輸出說。你能幫我解決這個錯誤嗎?獲取有關GridView的刷新刷新錯誤

這是我的活動類,給出錯誤。

public class ImageGridFragment extends AbsListViewBaseFragment { 

public static final int INDEX = 1; 

DisplayImageOptions options; 
private PullToRefreshGridView mPullRefreshGridView; 
List<String> imageUrls = getImages.IMAGE; 
GridView mGridView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    options = new DisplayImageOptions.Builder() 
      .showImageOnLoading(R.drawable.ic_stub) 
      .showImageForEmptyUri(R.drawable.ic_empty) 
      .showImageOnFail(R.drawable.ic_error) 
      .cacheInMemory(true) 
      .cacheOnDisk(true) 
      .considerExifParams(true) 
      .bitmapConfig(Bitmap.Config.RGB_565) 
      .build(); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.gridview_main, container, false); 

    mPullRefreshGridView = (PullToRefreshGridView) rootView.findViewById(R.id.gridview_); 
    mGridView = mPullRefreshGridView.getRefreshableView(); 

    mGridView.setAdapter(new ImageAdapter()); 

    mGridView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      startImagePagerActivity(position); 
     } 
    }); 
    mGridView.setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      startAddCollectionActivity(position); 
      return true; 
     } 
    }); 

    // Set a listener to be invoked when the list should be refreshed. 
    mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() { 

     @Override 
     public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) { 
      Toast.makeText(getActivity(), "Pull Down!", Toast.LENGTH_SHORT).show(); 
      new GetDataTask().execute(); 
     } 

     @Override 
     public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) { 
      Toast.makeText(getActivity(), "Pull Up!", Toast.LENGTH_SHORT).show(); 
      new GetDataTask().execute(); 
     } 

    }); 

    return rootView; 
} 

private List<String> getData() { 
    return imageUrls; 
} 

private class GetDataTask extends AsyncTask<Void, Void, List<String>> { 

    @Override 
    protected List<String> doInBackground(Void... params) { 
     // Simulates a background job. 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
     } 
     return imageUrls; 
    } 

    @Override 
    protected void onPostExecute(List<String> result) { 
     imageUrls.addAll(getData()); 
     ((BaseAdapter) mGridView.getAdapter()).notifyDataSetChanged(); 

     // Call onRefreshComplete when the list has been refreshed. 
     mPullRefreshGridView.onRefreshComplete(); 

     super.onPostExecute(result); 
    } 
} 


public class ImageAdapter extends BaseAdapter { 

    private LayoutInflater inflater; 

    ImageAdapter() { 
     inflater = LayoutInflater.from(getActivity()); 
    } 

    @Override 
    public int getCount() { 
     return imageUrls.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     View view = convertView; 
     if (view == null) { 
      view = inflater.inflate(R.layout.item_grid_image, parent, false); 
      holder = new ViewHolder(); 
      assert view != null; 
      holder.imageView = (ImageView) view.findViewById(R.id.image); 
      holder.progressBar = (ProgressBar) view.findViewById(R.id.progress); 
      view.setTag(holder); 
     } else { 
      holder = (ViewHolder) view.getTag(); 
     } 

     ImageLoader.getInstance() 
       .displayImage(imageUrls.get(position), holder.imageView, options, new SimpleImageLoadingListener() { 
        @Override 
        public void onLoadingStarted(String imageUri, View view) { 
         holder.progressBar.setProgress(0); 
         holder.progressBar.setVisibility(View.VISIBLE); 
        } 

        @Override 
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) { 
         holder.progressBar.setVisibility(View.GONE); 
        } 

        @Override 
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
         holder.progressBar.setVisibility(View.GONE); 
        } 
       }, new ImageLoadingProgressListener() { 
        @Override 
        public void onProgressUpdate(String imageUri, View view, int current, int total) { 
         holder.progressBar.setProgress(Math.round(100.0f * current/total)); 
        } 
       }); 

     return view; 
    } 
} 

static class ViewHolder { 
    ImageView imageView; 
    ProgressBar progressBar; 
} 


} 

這是日誌貓輸出:

02-08 21:17:37.953: E/AndroidRuntime(2495): FATAL EXCEPTION: main 
02-08 21:17:37.953: E/AndroidRuntime(2495): java.lang.NullPointerException 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at com.example.imageLoaderLibrary.ImageGridFragment.onCreateView(ImageGridFragment.java:78) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1073) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.support.v4.view.ViewPager.populate(ViewPager.java:919) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:681) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.View.measure(View.java:15172) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.Choreographer.doCallbacks(Choreographer.java:555) 
02-08 21:17:37.953: E/AndroidRuntime(2495):  at android.view.Choreographer.doFrame(Choreographer.java:525) 

編輯:此行得到錯誤:mPullRefreshGridView = (PullToRefreshGridView) rootView.findViewById(R.id.gridview_);

回答

0

您在onCreateView功能有一個錯誤,線路78 - 只有你能知道哪條線那是。

不過,我會猜測說,可能這行失敗:

mPullRefreshGridView = (PullToRefreshGridView) rootView.findViewById(R.id.gridview_); 
+0

對不起,我忘了說哪條線路得到錯誤。是的,你是真的,這條線給錯誤,但爲什麼?我在問,實際上:) – newComer 2015-02-08 21:53:50

+0

你確定佈局「gridview_main」的元素的ID爲「gridview_」? – atastrumf 2015-02-08 22:05:24

+0

是的,gridview_在gridview_main中。 – newComer 2015-02-08 22:08:11