android 传值显示listview中的item问题

本人初学Android,其中遇到一个问题,将一个值传入listview页面,但是运行时总是报错
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.xng.rkcamera.IpcameraAdapter.setDevices(java.util.List)' on a null object reference
at com.example.xng.rkcamera.CameraFragment.receive(CameraFragment.java:46)
at com.example.xng.rkcamera.MainActivity.parseUdpInfo(MainActivity.java:218)
at com.example.xng.rkcamera.MainActivity.access$300(MainActivity.java:31)
at com.example.xng.rkcamera.MainActivity$1.run(MainActivity.java:178)

 package com.example.xng.rkcamera;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xng on 2016/8/5.
 */
public class IpcameraAdapter extends BaseAdapter {
    public static final String TAG="IpcameraAdapter";
    private Context context;
    private List<IpcameraModel> list = new ArrayList<IpcameraModel>();

    public IpcameraAdapter(Context context)
    {
        super();
        this.context = context;
    }

    public IpcameraAdapter(Context context,List<IpcameraModel> list)
    {
        super();
        this.context = context;
        this.list=list;
    }

    public void setDevices(IpcameraModel m){
        this.list.add(m);
    }

    @Override
    public int getCount()
    {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public IpcameraModel getItem(int position)
    {
        if (list != null) {
            return list.get(position);
        }
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.ipcamera_item, null);
            viewHolder.videoView = (VideoView) convertView.findViewById(R.id.videoShow);
            viewHolder.mcamera = (ImageView) convertView.findViewById(R.id.image_camera);
            viewHolder.cameraname = (TextView) convertView.findViewById(R.id.cameraname);
            viewHolder.msettingbtn = (ImageButton) convertView.findViewById(R.id.image_setting_btn);
            viewHolder.mdownloadbtn = (ImageButton) convertView.findViewById(R.id.image_download_btn);
            viewHolder.mdelbtn = (ImageButton) convertView.findViewById(R.id.image_del_btn);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // set item values to the viewHolder:
        viewHolder.videoView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });
        viewHolder.cameraname.setText(list.get(position).getCameraname());
        viewHolder.msettingbtn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View view) {
                //响应事件
                Intent test = new Intent(context,AlbumFragment.class);
                context.startActivity(test);
            }
        });
        viewHolder.mdownloadbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent test = new Intent(context,AlbumFragment.class);
                context.startActivity(test);
            }
        });
        viewHolder.mdelbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent test = new Intent(context,AlbumFragment.class);
                context.startActivity(test);
            }
        });

        return convertView;
    }


    private static class ViewHolder
    {
        VideoView videoView;
        ImageView mcamera;
        TextView cameraname;
        ImageButton msettingbtn;
        ImageButton mdownloadbtn;
        ImageButton mdelbtn;

    }

}

package com.example.xng.rkcamera;

import android.provider.MediaStore;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.VideoView;

/**

  • Created by Xng on 2016/8/4.
    */
    public class IpcameraModel {
    private String cameraname;
    private int mdownloadbtn,mdelbtn;
    private int videoView;
    private ImageButton msettingbtn;

    public IpcameraModel(String cameraname) {
    this.cameraname = cameraname;
    }

    public String getCameraname() {return cameraname;}

    public void setCameraname(String cameraname) {
    this.cameraname = cameraname;
    }

    public ImageButton getMsettingbtn() {
    return msettingbtn;
    }

    public void setMsettingbtn(ImageButton msettingbtn) {
    this.msettingbtn = msettingbtn;
    }

    public int getMdownloadbtn(){
    return mdownloadbtn;
    }
    public void setMdownloadbtn(int mdownloadbtn){
    this.mdownloadbtn=mdownloadbtn;
    }

    public int getMdelbtn(){
    return mdelbtn;
    }
    public void setMdelbtn(int mdelbtn){
    this.mdelbtn=mdelbtn;
    }

    public int getVideoView(){
    return videoView;
    }

    public void setVideoView(int videoView){
    this.videoView=videoView;
    }
    }
    图片说明



package com.example.xng.rkcamera;

/**

  • Created by Xng on 2016/8/4. */ import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**

  • 相机的碎片页面
  • @author wwj_748
    *
    */
    public class CameraFragment extends Fragment {
    private ListView listView;
    private IpcameraAdapter ipcameraAdapter;
    private Context context;
    private List deviceList = new ArrayList();
    static final String TAG = "TEST";

    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    

    }

    public void receive(String msg){
    Log.d(TAG,"getSendResult:"+msg);

    IpcameraModel model = new IpcameraModel(msg);
    Log.d(TAG,"getSendRes:"+model);
    
    deviceList.add(model);
    Log.d(TAG,"mdeviceList:"+deviceList);
    
    ipcameraAdapter.setDevices(model);
    ipcameraAdapter.notifyDataSetChanged();
    

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.camera_fragment, container,
    false);
    listView=(ListView)view.findViewById(R.id.lvMember);

    ipcameraAdapter = new IpcameraAdapter(getActivity(),deviceList);
    Log.d(TAG,"getIP:"+ipcameraAdapter);
    
    listView.setAdapter(ipcameraAdapter);
    return view;
    

    }
    }

2个回答

自己看一下适配器是否实例化

receivek可能被先执行到了,里面ipcameraAdapter为null

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!