gridview adapter parent.getChildAt(position)返回nullPointerException

时间:2014-08-12 12:04:26

标签: android gridview broadcastreceiver

我有一个下载管理器,带有可下载项目的gridview,下载完成后我需要更新gridview按钮以显示用户下载已完成所以我使用调用适配器的BroadcastReceiver并通知它显示更改但我在父级中得到nullPointerException错误.getChildAt(位置)。我应该说我在一个片段和一个活动中使用相同的listview代码并且它确实工作得很好但是gridview存在一些问题。这是我的适配器getview:

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_special, null);

    TextView title = (TextView)vi.findViewById(R.id.pid); // title
    final TextView artist = (TextView)vi.findViewById(R.id.name); // artist name
    TextView duration = (TextView)vi.findViewById(R.id.price); // duration
    final TextView fileurl = (TextView)vi.findViewById(R.id.furl); // fileurl
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.image); // thumb image

    final TextView refrenceid = (TextView)vi.findViewById(R.id.dmrefrence); // refrence id

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(AllProductsActivity.TAG_PID));
    artist.setText(song.get(AllProductsActivity.TAG_NAME));
    duration.setText(song.get(AllProductsActivity.TAG_PRICE));
    fileurl.setText(song.get(AllProductsActivity.TAG_FILEURL));
    imageLoader.DisplayImage(song.get(AllProductsActivity.TAG_DESCRIPTION), thumb_image);

    final ImageButton pauseButton=(ImageButton)vi.findViewById(R.id.cancelButton); // image button

    final ImageButton imageButton=(ImageButton)vi.findViewById(R.id.imageButton1); // image button
    imageButton.setOnClickListener(new View.OnClickListener() {     
        @Override
        public void onClick(View arg0) {
            // starting background task to download product
            final String dfileurl = fileurl.getText().toString();
            final String fname = artist.getText().toString();

            imageButton.setVisibility( View.GONE );
            pauseButton.setVisibility( View.VISIBLE );
            SpecialFragment x = new SpecialFragment();

            long id = x.myClickdwnl(fname,dfileurl,pauseButton,activity.getApplicationContext());
            downloadIdToPositionMap.put(id, position);
            refrenceid.setText(Long.toString(id));

       }
    });
    /* Cancel Download */
    pauseButton.setOnClickListener(new View.OnClickListener() {     
        @Override
        public void onClick(View arg0) {
            // starting background task to download product
            final String dnid = refrenceid.getText().toString();
            pauseButton.setVisibility( View.GONE );
            imageButton.setVisibility( View.VISIBLE );
            HomeFragment x = new HomeFragment();

            x.Canceldwnl(dnid,activity.getApplicationContext());

       }
    });

    if(this.isDownloadFinished(position)){
       //ViewTreeObserver tree = parent.getViewTreeObserver();

       View v = parent.getChildAt(position);
       ImageButton cancelButton = (ImageButton) v.findViewById(R.id.cancelButton);
       ImageButton successButton = (ImageButton) v.findViewById(R.id.succButton);
       cancelButton.setVisibility( View.GONE );
       successButton.setVisibility( View.VISIBLE );

       for(int i=0; i<this.finishedDownloadPositions.size();i++){
           if(this.finishedDownloadPositions.get(i) == position){
               this.finishedDownloadPositions.remove(i);
           }
       }

    }

    return vi;
}

和我的BroadcastReceiver:

BroadcastReceiver griddownloadReceiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       //check if the broadcast message is for our Enqueued download
       long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

       //((GridAdapter)  getActivity().getListAdapter()).setDownloadFinished(referenceId);
       GridView gv1=(GridView)getView().findViewById(R.id.grid_view);
       adapter = (GridAdapter) gv1.getAdapter();
       //final GridAdapter adapter = (GridAdapter) ((GridView) getView()).getAdapter();

        adapter.setDownloadFinished(referenceId);
        adapter.notifyDataSetChanged();

       // Ask the adapter to refresh the ListView
       //adapter.notifyDataSetChanged();

      }
};//end of broadcast reciever

和logcat:

08-12 11:51:04.881: E/AndroidRuntime(850): FATAL EXCEPTION: main

08-12 11:51:04.881:E / AndroidRuntime(850):java.lang.NullPointerException 08-12 11:51:04.881:E / AndroidRuntime(850):at com.example.one.GridAdapter.getView(GridAdapter.java:123) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.AbsListView.obtainView(AbsListView.java:2143) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.GridView.makeAndAddView(GridView.java:1341) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.GridView.makeRow(GridView.java:341) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.GridView.fillSpecific(GridView.java:543) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.GridView.layoutChildren(GridView.java:1240) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.AbsListView.onLayout(AbsListView.java:1994) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.View.layout(View.java:14008) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.ViewGroup.layout(ViewGroup.java:4373) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.View.layout(View.java:14008) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.ViewGroup.layout(ViewGroup.java:4373) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.support.v4.view.ViewPager.onLayout(ViewPager.java:1528) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.View.layout(View.java:14008) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.ViewGroup.layout(ViewGroup.java:4373) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.View.layout(View.java:14008) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.ViewGroup.layout(ViewGroup.java:4373) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.View.layout(View.java:14008) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.ViewGroup.layout(ViewGroup.java:4373) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.View.layout(View.java:14008) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.ViewGroup.layout(ViewGroup.java:4373) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4351) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.Choreographer.doCallbacks(Choreographer.java:562) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.Choreographer.doFrame(Choreographer.java:532) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.os.Handler.handleCallback(Handler.java:725) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.os.Handler.dispatchMessage(Handler.java:92) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.os.Looper.loop(Looper.java:137) 08-12 11:51:04.881:E / AndroidRuntime(850):在android.app.ActivityThread.main(ActivityThread.java:5041) 08-12 11:51:04.881:E / AndroidRuntime(850):at java.lang.reflect.Method.invokeNative(Native Method) 08-12 11:51:04.881:E / AndroidRuntime(850):at java.lang.reflect.Method.invoke(Method.java:511) 08-12 11:51:04.881:E / AndroidRuntime(850):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 08-12 11:51:04.881:E / AndroidRuntime(850):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-12 11:51:04.881:E / AndroidRuntime(850):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

尝试使用上下文而不是getView()方法。

  BroadcastReceiver griddownloadReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   //check if the broadcast message is for our Enqueued download
   long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

   //((GridAdapter)  getActivity().getListAdapter()).setDownloadFinished(referenceId);
   GridView gv1 = (GridView) context.findViewById(R.id.grid_view);
   adapter = (GridAdapter) gv1.getAdapter();
   //final GridAdapter adapter = (GridAdapter) ((GridView) getView()).getAdapter();

    adapter.setDownloadFinished(referenceId);
    adapter.notifyDataSetChanged();

   // Ask the adapter to refresh the ListView
   //adapter.notifyDataSetChanged();

  }
};//end of broadcast reciever

如果这不起作用,请告诉我,我有另一个想法