我在这行代码中使用函数getScanRecord:
if (!(this.mScanResult.getScanRecord().getServiceData() == null || this.mScanResult.getScanRecord().getServiceData().size() == 0)) {
advertisementData = advertisementData + "Service Data: " + this.mScanResult.getScanRecord().getServiceData().toString().replace('{', ' ').replace('}', ' ') + "n";
}
它在Android Studio中给了我这个错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.catu.catucomm/com.catu.catucomm.Second}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.le.ScanRecord android.bluetooth.le.ScanResult.getScanRecord()' on a null object reference
我已经通过这样初始化变量解决了这个问题:
private ScanResult mScanResult;
public Second(ScanResult mScanResult)
{
this.mScanResult = mScanResult;
}
但是现在我有了新的问题:
. lang。InstantiationException: java.lang.Class
根据您的错误消息,我怀疑您的'mScanResult'变量为空。检查你的'mScanResult'是否为空
试试这个。
/*Listener sur le btn Pair*/
mPairBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Second.class);
intent.putExtra(EXTRAS_SCAN_RESULT, -YOU NEED TO PUT YOUR SCAN RESULT HERE- );
startActivity(intent);
finish();
}
});
您需要使用putExtra()函数将扫描结果对象传递给第二个活动。