Android 设置列表项的值时自定义适配器中出现空指针异常

Android 设置列表项的值时自定义适配器中出现空指针异常,android,android-listview,model,custom-adapter,Android,Android Listview,Model,Custom Adapter,当用户请求或单击按钮时,我想在列表视图中显示一些条件数据。每个列表行有两个值,一个值是名称,另一个值是说明。当用户按下按钮时,它触发命令并从外围设备获取信息,并逐个匹配不同条件下的所有值,因为条件与值匹配,所以在所有条件检查后,它将在适配器中放置一个列表行值,并将适配器分配给列表视图,然后在列表视图中显示所有适配器数据 这是我的密码 公共类DTCode扩展活动{ private final static String TAG=DTCode.class.getSimpleName() 最终静态字符

当用户请求或单击按钮时,我想在列表视图中显示一些条件数据。每个列表行有两个值,一个值是名称,另一个值是说明。当用户按下按钮时,它触发命令并从外围设备获取信息,并逐个匹配不同条件下的所有值,因为条件与值匹配,所以在所有条件检查后,它将在适配器中放置一个列表行值,并将适配器分配给列表视图,然后在列表视图中显示所有适配器数据

这是我的密码

公共类DTCode扩展活动{ private final static String TAG=DTCode.class.getSimpleName()

最终静态字符串EXTRA\u DEVICE\u ADDRESS=“DEVICE\u ADDRESS”;
公共字符串mDeviceAddress;
公共蓝牙服务mBluetoothLeService;
私有布尔值mConnected=false;
按钮btnread,btnclear;
字符串receiveData=“”;
列表视图myList;
错误适配器;
ArrayList ArrayOferors;
误差;
列表视图列表错误;
//获取服务的代码;
专用最终ServiceConnection mServiceConnection=新ServiceConnection(){
@凌驾
服务连接上的公共无效(组件名称组件名称,IBinder服务){
mBluetoothLeService=((BluetoothLeService.LocalBinder)服务).getService();
如果(!mBluetoothLeService.initialize()){
Log.e(标记“蓝牙未初始化”);
完成();
}
Log.e(标签“蓝牙服务正常”);
mBluetoothLeService.connect(mDeviceAddress);
}
@凌驾
ServiceDisconnected上的公共无效(ComponentName ComponentName){
mBluetoothLeService=null;
}
};
private final BroadcastReceiver mGattUpdateReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
最后一个字符串action=intent.getAction();
if(蓝牙服务动作\关贸总协定\连接的相等动作){
Log.e(标签“只有关贸总协定,只需等待”);
}else if(蓝牙服务操作\u GATT\u断开连接。等于(操作)){
mConnected=false;
无效操作菜单();
btnclear.setEnabled(false);
btnread.setEnabled(false);
}else if(BluetoothLeService.ACTION\u GATT\u SERVICES\u DISCOVERED.equals(ACTION)){
mConnected=true;
btnread.setEnabled(真);
Log.e(标记为“我们需要什么”);
}else if(BluetoothLeService.ACTION\u DATA\u AVAILABLE.equals(ACTION)){
Log.e(标签“接收数据”);
字符串数据=intent.getStringExtra(BluetoothLeService.EXTRA_数据);
如果(数据!=null){
btnclear.setEnabled(true);
receiveData=receiveData.concat(数据);
char character=receiveData.charAt(receiveData.length()-1);
系统输出打印项次(接收数据);
如果(字符=='\n'){
字符串[]err=receiveData.split(“!”);
for(int i=0;ifinal static String EXTRA_DEVICE_ADDRESS = "DEVICE_ADDRESS";

public  String mDeviceAddress;
public BluetoothLeService mBluetoothLeService;
private boolean mConnected = false;

Button btnread, btnclear;
String receiveData = "";
ListView myList;

ErrorsAdapter adapter;

ArrayList<Error> arrayofErrors;

Error error;

ListView listError;
//Code for get Service;

private  final ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
        if (!mBluetoothLeService.initialize()){
            Log.e(TAG, "Bluetooth is not initialize");
            finish();
        }
        Log.e(TAG,"Bluetooth Service is oKay");
        mBluetoothLeService.connect(mDeviceAddress);
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        mBluetoothLeService = null;
    }
};

private final BroadcastReceiver mGattUpdateReceiver= new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final  String action = intent.getAction();
        if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)){
            Log.e(TAG, "Only Gatt, Just Wait");
        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)){
            mConnected = false;
            invalidateOptionsMenu();
            btnclear.setEnabled(false);
            btnread.setEnabled(false);
        }else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)){
            mConnected =true;
            btnread.setEnabled(true);
            Log.e(TAG,"In what we need");
        }else  if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)){
            Log.e(TAG,"Receive Data");

            String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);

            if (data != null){
                btnclear.setEnabled(true);
                receiveData = receiveData.concat(data);

                char character = receiveData.charAt(receiveData.length() - 1 );
                System.out.println(receiveData);

                if (character == '\n'){

                    String[] err = receiveData.split("!");

                    for (int i = 0 ; i < err.length ; i++) {

                        String[] errCode = err[i].split("_");

                        for (int j = 0; j < errCode.length; j++) {

                            System.out.println(errCode[j]);

                            if (errCode[j].startsWith("108E07")){
                                error = new Error("DIM-B108E07", "Display failure Information");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("115C71")){
                                error = new Error("DIM-B115C71", "Transfer Fuel Pump. Actuator Stuck");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("1A0107")){
                                error = new Error("DIM-B1A0107","Speaker #1 Failure.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("1B6916")){
                                error = new Error("DIM-B1B6916","Circuit Voltage Below Threshold.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("1B6917")){
                                error = new Error("DIM-B1B6917","Circuit Voltage above Threshold");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("1A4815")){
                                error = new Error("DIM-C1A4815", "Short Circuit to Battery or Open.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("600262")){
                                error = new Error("DIM-C200262", "Odometer Tempered");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("046071")){
                                error = new Error("DIM-P0460171","Fuel Level A Circuit, Actuator Stuck.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("134671")){
                                error = new Error("DIM-P134671","Fuel Level B Circuit, Actuator Stuck.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C00155")){
                                error = new Error("DIM-U000155","High Speed can not Configured.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C00188")){
                                error = new Error("DIM-U000188","High Speed Can Bus Off.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C01055")){
                                error = new Error("DIM-U001055","Medium Speed Can Not Configured.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C01088")){
                                error = new Error("DIM-U001088","Medium Speed Can Bus Off.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C10000")){
                                error = new Error("DIM-U010000","Lost Communication with ECM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C10100")){
                                error = new Error("DIM-U010100","Lost Communication with TCM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C10400")){
                                error = new Error("DIM-U010400","Lost Communication with FSM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C12100")){
                                error = new Error("DIM-U012100","Lost Communication with ABS");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C14000")){
                                error = new Error("DIM-U014000","Lost Communication with CEM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C15100")){
                                error = new Error("DIM-U015100","Lost Communication with SRSM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C15600")){
                                error = new Error("DIM-U015600","Lost Communication with ICM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C15900")){
                                error = new Error("DIM-U015900","Lost Communication with PAM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C25000")){
                                error = new Error("DIM-U025000","Lost Communication with PPM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C25200")){
                                error = new Error("DIM-U025200","Lost Communication with TRM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C40168")){
                                error = new Error("DIM-U040168","Invalid Data Received from ECM/PCM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C40268")){
                                error = new Error("DIM-U040268","Invalid Data Received from TCM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C41568")){
                                error = new Error("DIM-U041568","Invalid Data Received from ABS.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C42268")){
                                error = new Error("DIM-U042268","Invalid Data Received from CEM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C45292")){
                                error = new Error("DIM-U045292","Invalid Data Received from SRS.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("C55168")){
                                error = new Error("DIM-U055168","Invalid Data Received from PPM.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("DA4B49")){
                                error = new Error("DIM-U1A4B49","Internal Electronic Failure.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("E10000")){
                                error = new Error("DIM-U210000","Control Module not Configured.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("E10100")){
                                error = new Error("DIM-U210100","Control Module Configuration Incompatible.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("F00044")){
                                error = new Error("DIM-U300044","Data Memory failure.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("F00045")){
                                error = new Error("DIM-U300045","Program Memory Failure.");
                                adapter.add(error);
                            }else if (errCode[j].startsWith("F00362")){
                                error = new Error("DIM-U300362","Signal Compare Failure.");
                                adapter.add(error);
                            }
                        }
                    }
                  listError.setAdapter(adapter);
                }
            }
        }

    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dtcode);

    final Intent intent = getIntent();
    mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);

    btnread = (Button) findViewById(R.id.read);
    btnread.setEnabled(false);

    btnclear= (Button) findViewById(R.id.clr);
    btnclear.setEnabled(false);

    arrayofErrors = new ArrayList<Error>();
    adapter = new ErrorsAdapter(this, arrayofErrors);

    listError = (ListView) findViewById(R.id.list);

    getActionBar().setTitle("TFT Trouble Code");
    getActionBar().setDisplayHomeAsUpEnabled(true);
    Log.d(TAG, "Try to bindService=" + bindService(new Intent(this, BluetoothLeService.class), mServiceConnection, BIND_AUTO_CREATE));

    registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());

}

@Override
protected void onResume(){
    super.onResume();
    registerReceiver(mGattUpdateReceiver,makeGattUpdateIntentFilter());
    if (mBluetoothLeService != null){
        final boolean result = mBluetoothLeService.connect(mDeviceAddress);
        Log.d(TAG,"Connect Result="+ result);
    }
}

@Override
protected void onPause(){
    super.onPause();
    unregisterReceiver(mGattUpdateReceiver);
    unbindService(mServiceConnection);
}

@Override
protected  void  onDestroy(){

    if (mBluetoothLeService != null){
        mBluetoothLeService.close();
        mBluetoothLeService = null;
    }
    Log.d(TAG, "We are in Destroy");
}

public void onClick (View v){

    if (v == btnread){
        if(!mConnected) return;
        mBluetoothLeService.WriteValue("$DIM_RE\n");
    }
    if (v == btnclear){
        if(!mConnected) return;
        mBluetoothLeService.WriteValue("$DIM_CLR\n");
        myList.setAdapter(null);
    }
}


public  class  ErrorsAdapter extends  ArrayAdapter<Error>{
    public ErrorsAdapter(Context context, ArrayList<Error> errors){
        super(context, 0, errors);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        Error error = getItem(position);

        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_error, parent, false);
        }

        TextView ecTxt = (TextView) findViewById(R.id.txt_error_code);
        TextView edTxt = (TextView) findViewById(R.id.txt_error_description);

        ecTxt.setText(error.errorCode);
        edTxt.setText(error.errorDescription);

        return convertView;
    }
}
TextView ecTxt = (TextView) findViewById(R.id.txt_error_code);
TextView edTxt = (TextView) findViewById(R.id.txt_error_description);
TextView ecTxt = (TextView) convertView.findViewById(R.id.txt_error_code);
TextView edTxt = (TextView) convertView.findViewById(R.id.txt_error_description);