android - 为列表项设置值时自定义适配器中的空指针异常

标签 android android-listview model custom-adapter

我想在用户请求或单击按钮时在 ListView 中显示一些条件数据。每个列表行都有两个值,一个值是名称,第二个是它的描述。当用户按下按钮时,它会触发命令并从外设获取信息,并逐个匹配具有不同条件的所有值,因为条件与值匹配,它会在所有条件检查后将一个列表行值放入适配器,它将适配器分配给 ListView ,然后显示所有适配器数据将显示在 ListView 中。

这是我的代码

公共(public)类 DTCode 扩展 Activity { private final static String TAG = DTCode.class.getSimpleName();

final 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;
    }
}

最佳答案

在适配器的 getView 中,替换

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);

关于android - 为列表项设置值时自定义适配器中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30759301/

相关文章:

android - 使用 AppWidgetManager 的 updateAppWidgetOptions 方法来存储每个小部件的自定义选项

java - Android 后退按钮导致 Bad Keycharmap 警告

java - 在 onPreExecute() 内部调用时 TextView.setText() 的 NullPointerExcepction

Android,适配器中的 RelativeLayout 导致错误

java - 如何删除 Android 回收器 View 中删除项目后留下的空白空间?

java - 如果使用 Android 的两个项目具有相同的图像,则隐藏 GridView 中的项目

android - Activity 中的 QuickReturn ListView? Activity 中的 2 个 View 有困难

python - 我如何在 Django 中模拟这种行为?

database - Sequelize验证抛出错误

Django 模型在同步数据库后未显示在数据库中